【问题标题】:angularJs - unit testing using jasmine. How to delete a spy ? Getting Error: <spyOn> : getList has already been spied uponangularJs - 使用 jasmine 进行单元测试。如何删除间谍?出现错误:<spyOn>:getList 已被监视
【发布时间】: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


    【解决方案1】:

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

    您的 jasmine 测试将运行,但是当您启动应用程序时,如果您不将随机字符串和空函数作为 args 放入 createSpy(),您的应用程序打字稿会大声对您大喊大叫。

    和平

    【讨论】:

    • 对不起,但我想获得更多关于修改/删除间谍的见解。我的茉莉花测试运行良好。另外我目前不在打字稿上。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多