How do we clear spy programmatically in Jasmine?

后端 未结 10 2382
孤独总比滥情好
孤独总比滥情好 2020-12-15 02:11

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

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


        
10条回答
  •  無奈伤痛
    2020-12-15 02:55

    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.

提交回复
热议问题