How do we clear spy programmatically in Jasmine?

JavascriptJasmine

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

setting isSpy to false is a very bad idea, since then you spy on a spy and when Jasmine clears the spies at the end of your spec you won't get the original method. the method will be equal to the first spy.

if are already spying on a method and you want the original method to be called instead you should call andCallThrough() which will override the first spy behavior.

for example

var spyObj = spyOn(obj,'methodName').andReturn(true);
spyObj.andCallThrough();

you can clear all spies by calling this.removeAllSpies() (this - spec)

Solution 2 - Javascript

I think that's what .reset() is for:

spyOn($, 'ajax');

$.post('http://someUrl', someData);

expect($.ajax).toHaveBeenCalled();

$.ajax.calls.reset()

expect($.ajax).not.toHaveBeenCalled();

Solution 3 - Javascript

So spies are reset automatically between specs.

You actually do not get the benefit of "restoration" of the original function if you use andCallFake() within a beforeEach() and then attempt to forcibly change it within a spec (which is likely why it tries to prevent you from doing so).

So be careful, especially if your spy is being set on a global object such as jQuery.

Demonstration:

var a = {b:function() { return 'default'; } }; // global scope (i.e. jQuery)
var originalValue = a.b;

describe("SpyOn test", function(){
  it('should return spy1', function(){
    spyOn(a, 'b').andCallFake(function(params) {
      return 'spy1';
    })
    expect(a.b()).toEqual('spy1');
  });

  it('should return default because removeAllSpies() happens in teardown', function(){
    expect(a.b()).toEqual('default');
  });


  it('will change internal state by "forcing" a spy to be set twice, overwriting the originalValue', function(){
    expect(a.b()).toEqual('default');

    spyOn(a, 'b').andCallFake(function(params) {
      return 'spy2';
    })
    expect(a.b()).toEqual('spy2');

    // This forces the overwrite of the internal state
    a.b.isSpy = false;
    spyOn(a, 'b').andCallFake(function(params) {
      return 'spy3';
    })
    expect(a.b()).toEqual('spy3');

  });

  it('should return default but will not', function(){
    expect(a.b()).toEqual('default'); // FAIL

    // What's happening internally?
    expect(this.spies_.length).toBe(1);
    expect(this.spies_[0].originalValue).toBe(originalValue); // FAIL
  });

});

describe("SpyOn with beforeEach test", function(){
  beforeEach(function(){
    spyOn(a, 'b').andCallFake(function(params) {
      return 'spy1';
    })
  })

  it('should return spy1', function(){
    // inspect the internal tracking of spies:
    expect(this.spies_.length).toBe(1);
    expect(this.spies_[0].originalValue).toBe(originalValue);

    expect(a.b()).toEqual('spy1');
  });

  it('should return spy2 when forced', function(){
    // inspect the internal tracking of spies:
    expect(this.spies_.length).toBe(1);
    expect(this.spies_[0].originalValue).toBe(originalValue);

    // THIS EFFECTIVELY changes the "originalState" from what it was before the beforeEach to what it is now.
    a.b.isSpy = false;
    spyOn(a, 'b').andCallFake(function(params) {
        return 'spy2';
    })
    expect(a.b()).toEqual('spy2');
  });

  it('should again return spy1 - but we have overwritten the original state, and can never return to it', function(){
    // inspect the internal tracking of spies:
    expect(this.spies_.length).toBe(1);
    expect(this.spies_[0].originalValue).toBe(originalValue); // FAILS!

    expect(a.b()).toEqual('spy1');
  });
});

// If you were hoping jasmine would cleanup your mess even after the spec is completed...
console.log(a.b == originalValue) // FALSE as you've already altered the global object!

Solution 4 - Javascript

In Jasmine 2, the spy state is held in a SpyStrategy instance. You can get hold of this instance calling $.ajax.and. See the Jasmine source code on GitHub.

So, to set a different fake method, do this:

$.ajax.and.callFake(function() { ... });

To reset to the original method, do this:

$.ajax.and.callThrough();

Solution 5 - Javascript

This worked for me in Jasmine 2.5 to allow re-setting of mock ajax.

function spyOnAjax(mockResult) {
    // must set to true to allow multiple calls to spyOn:
    jasmine.getEnv().allowRespy(true);

    spyOn($, 'ajax').and.callFake(function () {
        var deferred = $.Deferred();
        deferred.resolve(mockResult);
        return deferred.promise();
    });
}

Then you can call it multiple times without error. spyOnAjax(mock1); spyOnAjax(mock2);

Solution 6 - Javascript

From jasmine 2.5, you can use this global setting to update a spy within your test cases:

jasmine.getEnv().allowRespy(true);

Solution 7 - Javascript

Or you can do it

describe('test', function() {
    var a, c;
    c = 'spy1';
    a = {
      b: function(){}
    };

    beforeEach(function() {
        spyOn(a, 'b').and.callFake(function () {
             return c;
        });
    })

    it('should return spy1', function() {
        expect(a.b()).toEqual('spy1');
    })

    it('should return spy2', function() {
        c = 'spy2';
        expect(a.b()).toEqual('spy2');
    })

})

In this case you use the same Spy but just change the var that it will return..

Solution 8 - Javascript

I'm posting this answer to address the comment in OP @Tri-Vuong's code - which was my main reason for my visiting this page:

> I want to override the spy ... here and do it a little differently

None of the answers so far address this point, so I'll post what I've learned and summarize the other answers as well.

@Alissa called it correctly when she explained why it is a bad idea to set isSpy to false - effectively spying on a spy resulting in the auto-teardown behavior of Jasmine no longer functioning as intended. Her solution (placed within the OP context and updated for Jasmine 2+) was as follows:

beforeEach(() => {
  var spyObj = spyOn(obj,'methodName').and.callFake(function(params){
  }) // @Alissa's solution part a - store the spy in a variable
})

it("should do the declared spy behavior", () => {
  // Act and assert as desired
})

it("should do what it used to do", () => {
  spyObj.and.callThrough(); // @Alissa's solution part b - restore spy behavior to original function behavior
  // Act and assert as desired
})

it("should do something a little differently", () => {
  spyObj.and.returnValue('NewValue'); // added solution to change spy behavior
  // Act and assert as desired
})

The last it test demonstrates how one could change the behavior of an existing spy to something else besides original behavior: "and-declare" the new behavior on the spyObj previously stored in the variable in the beforeEach(). The first test illustrates my use case for doing this - I wanted a spy to behave a certain way for most of the tests, but then change it for a few tests later.

For earlier versions of Jasmine, change the appropriate calls to .andCallFake(, .andCallThrough(), and .andReturnValue( respectively.

Solution 9 - Javascript

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.

Solution 10 - Javascript

just set the spy method to null

mockedService.spiedMethod = null;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontrivektorView Question on Stackoverflow
Solution 1 - JavascriptAlissaView Answer on Stackoverflow
Solution 2 - JavascriptGxXcView Answer on Stackoverflow
Solution 3 - JavascriptFilmJView Answer on Stackoverflow
Solution 4 - JavascripttitusdView Answer on Stackoverflow
Solution 5 - Javascriptuser7054363View Answer on Stackoverflow
Solution 6 - JavascriptHamzeen HameemView Answer on Stackoverflow
Solution 7 - JavascriptRafaCianciView Answer on Stackoverflow
Solution 8 - JavascriptLHMView Answer on Stackoverflow
Solution 9 - JavascriptAndreas KöberleView Answer on Stackoverflow
Solution 10 - JavascriptdasAnderl ausMingaView Answer on Stackoverflow