【发布时间】:2017-05-10 16:22:30
【问题描述】:
我正在尝试为我的控制器中使用的特定角度服务方法编写单元测试用例。
我的 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
如何删除间谍?对于至少 10 个类似“it”块中提到的条件,我仍然需要测试相同的 successCb 函数。
【问题讨论】:
标签: angularjs unit-testing karma-jasmine