使用Jasmine进行的角单元测试:如何移除或修改spyOn

时间:2022-08-24 21:51:13

AngularJS v1.2.26

AngularJS v1.2.26

Jasmine v2.2.0

茉莉花v2.2.0

How can I change or remove the behavior of a spyOn? When I try to override it, I get the following error: Error: getUpdate has already been spied upon

我怎样才能改变或消除一个spyOn的行为?当我试图重写它时,我得到了以下错误:错误:getUpdate已经被监视了。

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

          ...

        });
    });

...

When I remove the second spyOn the test doesn't work.

当我移除第二个spyOn时,这个测试不成立。

How do I do this?

我该怎么做呢?

5 个解决方案

#1


50  

You can just overwrite it

你可以重写它

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

#2


16  

You can override the return value of the spy

您可以覆盖该间谍的返回值

    var deferred = $q.defer();
    deferred.resolve( data1 );

    var getUpdateSpy = spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);



    var newDeferred = $q.defer();
    newDeferred.resolve( data2 );

    getUpdateSpy.and.returnValue(newDeferred.promise);        

#3


2  

the green check-marked answer didn't work for me, but this did:

绿色标注的答案对我不起作用,但它起了作用:

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

your jasmine test will run but when you go to fire up your app typescript will yell loudly at you if you don't put a random string and an empty function as the args to createSpy().

您的茉莉花测试将会运行,但是当您启动您的应用程序时,如果您不将一个随机字符串和一个空函数作为args来创建createSpy(),那么打字稿将会大声地对您喊叫。

#4


1  

More easier way is to simple

更简单的方法是简单。

updateService.getUpdate.and.returnValue(Observable.of({status:true}));

#5


0  

Since jasmine v2.5, use the global allowRespy() setting.

由于jasmine v2.5,使用全局allowRespy()设置。

jasmine.getEnv().allowRespy(true);

You'll be able to call spyOn() multiple times, when you don't want and/or have access to the first spy. Beware it will return the previous spy, if any is already active.

您将能够多次调用spyOn(),当您不想和/或能够访问第一个间谍时。小心它会返回之前的密探,如果有的话。

spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);
...
spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);

#1


50  

You can just overwrite it

你可以重写它

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

#2


16  

You can override the return value of the spy

您可以覆盖该间谍的返回值

    var deferred = $q.defer();
    deferred.resolve( data1 );

    var getUpdateSpy = spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);



    var newDeferred = $q.defer();
    newDeferred.resolve( data2 );

    getUpdateSpy.and.returnValue(newDeferred.promise);        

#3


2  

the green check-marked answer didn't work for me, but this did:

绿色标注的答案对我不起作用,但它起了作用:

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

your jasmine test will run but when you go to fire up your app typescript will yell loudly at you if you don't put a random string and an empty function as the args to createSpy().

您的茉莉花测试将会运行,但是当您启动您的应用程序时,如果您不将一个随机字符串和一个空函数作为args来创建createSpy(),那么打字稿将会大声地对您喊叫。

#4


1  

More easier way is to simple

更简单的方法是简单。

updateService.getUpdate.and.returnValue(Observable.of({status:true}));

#5


0  

Since jasmine v2.5, use the global allowRespy() setting.

由于jasmine v2.5,使用全局allowRespy()设置。

jasmine.getEnv().allowRespy(true);

You'll be able to call spyOn() multiple times, when you don't want and/or have access to the first spy. Beware it will return the previous spy, if any is already active.

您将能够多次调用spyOn(),当您不想和/或能够访问第一个间谍时。小心它会返回之前的密探,如果有的话。

spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);
...
spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);