gpt4 book ai didi

javascript - 使用 Jasmine : how to remove or modify spyOn 进行 Angular 单元测试

转载 作者:IT王子 更新时间:2023-10-29 03:03:20 28 4
gpt4 key购买 nike

AngularJS v1.2.26

Jasmine v2.2.0

如何更改或删除 spyOn 的行为?当我尝试覆盖它时,出现以下错误:Error: getUpdate has already been spired upon

var data1 = 'foo';
var data2 = 'bar';

describe("a spec with a spy", function(){

beforeEach(module('app'));

var $q;

beforeEach(inject(function(_updateService_, _$q_){
updateService = _updateService_;

//spy the results of the getUpdate()
$q = _$q_;
var deferred = $q.defer();
deferred.resolve( data1 );
spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);

}));

describe('and here the spy should be different', function() {

it('returns a different value', function() {

var deferred = $q.defer();
deferred.resolve( data2 );
spyOn(updateService, 'getUpdate'); //ERROR HERE
updateService.getUpdate.and.returnValue(deferred.promise);

...

});
});

...

当我删除第二个 spyOn 时,测试不起作用。

我该怎么做?

最佳答案

你可以覆盖它

updateService.getUpdate = jasmine.createSpy().and.returnValue(etc)

关于javascript - 使用 Jasmine : how to remove or modify spyOn 进行 Angular 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28821511/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com