我们如何在Jasmine中以编程方式清除间谍?

我们如何在Jasmine中以编程方式清除间谍?

问题描述:

我们如何以编程方式清除茉莉花测试套件中的间谍?谢谢。

How do we clear the spy in a jasmine test suite programmatically? Thanks.

beforeEach(function() {
  spyOn($, "ajax").andCallFake(function(params){
  })
})

it("should do something", function() {
  //I want to override the spy on ajax here and do it a little differently
})


我不确定它是不是一个好主意,但你只需将函数上的 isSpy 标志设置为false:

I'm not sure if its a good idea but you can simply set the isSpy flag on the function to false:

describe('test', function() {
    var a = {b: function() {
    }};
    beforeEach(function() {
        spyOn(a, 'b').andCallFake(function(params) {
            return 'spy1';
        })
    })
    it('should return spy1', function() {
        expect(a.b()).toEqual('spy1');
    })

    it('should return spy2', function() {
        a.b.isSpy = false;
        spyOn(a, 'b').andCallFake(function(params) {
            return 'spy2';
        })
        expect(a.b()).toEqual('spy2');
    })

})

但也许它更好想要为这种情况创建一个新套件,你需要间谍的其他行为。

But maybe its a better idea to create a new suite for this case where you need an other behavior from your spy.