angularJs - 使用茉莉花进行单元测试。如何删除间谍?获取错误:<spyon>:getList已经被

时间:2017-05-10 16:22:30

标签: angularjs unit-testing karma-jasmine

我正在尝试为我的控制器中使用的特定角度服务方法编写单元测试用例。

我的 getListCtrlSpec.js 文件位于下方,

    /**
 * Spec Suite for "getListCtrl" controller
 */

describe('Controller : getListCtrl', function () {
    /* Load the reuqired module */
    beforeEach(module('myApp'));
    /*Initialize the variables */
    var getListCtrl,
        scope,
        $rootScope,
        someService
// Provide will help us create fake implementations for our dependencies
beforeEach(function () {
    module(function ($provide) {
        $provide.service('someService', function () {

        });

    });
});
/* Initilaize the controller and mock a scope */
beforeEach(inject(function (_$rootScope_, _$controller_,_someService_,) {
    $rootScope = _$rootScope_;
    $scope = scope = $rootScope.$new();
    $controller = _$controller_;


    getListCtrl = $controller('getListCtrl', {
        $scope: scope,
        someService: _someService_
    });

someService.getList = jasmine.createSpy('getList').and.returnValue([{}]);
}));

/* Spec To check if the controller is instantaiated */
it('should get an instance of getListCtrl - ', function () {
    expect(getListCtrl).toBeDefined();
});

it('should check for successCb function for getList method for Asia - ', function () {
    spyOn(someService, 'getList').and.returnValue(listForAsianCountries);
    var data = listForAsianCountries;
    scope.getListSuccessCb(data);
});

 it('should check for successCb function for getList method for Europe - ', function () {
    spyOn(someService, 'getList').and.returnValue(listForEuropeanCountries);
    var data = listForEuropeanCountries;
    scope.getListSuccessCb(data);
});
});

我收到错误

Error: <spyOn> : getList has already been spied upon

如何删除间谍?我还需要测试相同的successCb函数,至少还有10个条件,比如“it”块中提到的条件。

1 个答案:

答案 0 :(得分:-3)

yourCoolService.createThing = jasmine.createSpy('notreal', function(){}).and.returnValue();

你的茉莉花测试将会运行,但是如果你没有将随机字符串和空函数作为createSpy()的参数,那么当你开始点击你的app打字稿时,你会大声喊叫。

和平