0

How do i pass below list of parameters to the below unit test spec in jasmine?

params - "userId" : "user1", "location" : "london", "salary" : "33333", "hobby" : "swimming"

This is an approximate unit test spec and not an accurate one.

describe("my jasmine test suite", function() {

   var params,
       myController

   beforeEach(function() {
     module('myApp');
     mockService = jasmine.createSpyObj('mockService',['serviceCall']);

     module(function($provide) {

          $provide.value('myService',mockService);

       });

     inject(function($controller,$q,_$rootscope_) {
     mockService.serviceCall.and.callFake(function(srvcname,mthdname,params)
       {  
         var resData = {};
        if(mthdname === 'servicecall')
         {
            // assign the mockdata
          }
            return mockData;
     });

     $rootscope = _$rootscope_;
     myScope = $rootscope.$new();

    myController = $controller('myController',{$scope:myscope});
 });

});

it('my test spec',function() {

    expect(mockService.serviceCall).toHaveBeenCalled();

    expect(myscope.searchRslt[0]).toBe(mockData.searchRslt[0]);
    $rootScope.$digest();

});

});

I am not sure how do pass the parameter to the jasmine unit test spec

4

1 回答 1

0

如果你这样做...

mockService.serviceCall.and.callFake(function(srvcname,mthdname,params)
{  
    var mockData = {};
    if(mthdname === 'servicecall')
    {
        // assign the mockdata
        mockData.searchRslt = new Array();
        mockData.searchRslt.push({ "userId" : "user1", "location" : "london", "salary" : "33333", "hobby" : "swimming"});
    }
    return mockData;
 });

...的结果mockData.searchRslt[0]将是Object {userId: "user1", location: "london", salary: "33333", hobby: "swimming"}

由于我们不知道 的形状myscope.searchRslt[0],因此很难说这是否正是您所需要的,但它应该让您知道如何处理它。

于 2016-03-29T04:28:50.780 回答