Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Jasmine does not reset spy after each test suite #267

Closed
sourabhchaki opened this Issue · 2 comments

3 participants

@sourabhchaki

I have the following spec.

describe("SN.ExitHistory", function() {

var exitHistory;

beforeEach(function() {

SN.Utils = jasmine.createSpy("utils").andCallFake(function() {
function readSNCookie(cookieName, key) {
return "google.com";
}

function isUndefinedOrNull(param) {
return (param == null) || (param === "null");
}

function createSNCookie(snCookieName, key, value, lifeTime) {

}

var me = {
readSNCookie : readSNCookie,
isUndefinedOrNull : isUndefinedOrNull,
createSNCookie : createSNCookie
};
return me;

})();
exitHistory = SN.ExitHistory();

});

it("return last exit link", function() {
expect(exitHistory.getLastExitLink()).toEqual("google.com");
});

});

exitHistory.getLastExitLink internally uses SN.Utils.

After the test is done Jasmine does not remove the spy object utils. In next test suite also I can see the same utils present. Is there any way to reset the spy object after each test is done?

Instead of creating spy, if I create a new object for utils, behavior is same. Then what is the difference between a spy and actual object in this scenario.

Correct me if I am wrong.

@lukeasrodgers

I can't speak to the spy teardown, which should be happening, but in this exact case it looks like there is no reason to use a jasmine spy, though I'm not clear on exactly what you're trying to test. Perhaps you would just want to do something like this: spyOn(SN.Utils, 'readSNCookie').andReturn('google.com');.

Stubbing the other methods seems unnecessary, unless that code isn't loaded for this test suite.

@infews
Owner

This issue is that you're creating a spy as its own variable. These are NOT torn down between tests. But they should fall out of scope depending on how they are declared.

If you create an object and then spy on a function property of that object, we do tear down the spy.

But I agree with Luke, that you should probably test what you're doing differently.

Closing for now as this is a tutorial issue, not a Jasmine core issue.

Thanks for using Jasmine.

@infews infews closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.