如何在Jasmine单元测试中传递参数?

时间:2016-03-28 23:49:58

标签: javascript angularjs jasmine karma-jasmine ngmock

如何将以下参数列表传递给jasmine中的以下单元测试规范?

  

params - " userId" :" user1"," location" :"伦敦","薪水" :" 33333","业余爱好" :"游泳"

这是一个近似的单位测试规范而不是准确的。

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();

});

});

我不确定如何将参数传递给jasmine单元测试规范

1 个答案:

答案 0 :(得分: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]的形状,很难说它是否恰好符合您的需求,但它应该让您知道如何处理它