admin管理员组

文章数量:1292978

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";
        }

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

 });

exitHistory.getLastExitLink internally use 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.

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.";
        }

        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.");
    });

 });

exitHistory.getLastExitLink internally use 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.

Share Improve this question edited Jul 18, 2013 at 16:39 mor 2,31318 silver badges28 bronze badges asked Aug 10, 2012 at 8:35 SourabhSourabh 6803 gold badges8 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

I had the same problem some time ago and after days of struggling I found the solution. If you use the other way your spy will be reseted, so try with

spyOn(SN, 'Utils');

Spies are described here: https://github./pivotal/jasmine/wiki/Spies

Use spyOn and declare your spies within a before block inside of a describe spec block and the spies will be cleaned up when each spec is torn down.

aSpec.js

describe('something', () => {
    beforeAll(() => spyOn(someObject, 'someMethod').and.returnValue('foo'));
    it('is spied on', () => {
       expect(someObject.someMethod()).toEqual('foo');
    });
});

anotherSpec.js

describe('something else', () => {
    beforeAll(() => spyOn(someObject, 'someMethod').and.returnValue('bar'));
    it('is spied on', () => {
       expect(someObject.someMethod()).toEqual('bar');
    });
});

本文标签: javascriptJasmine does not reset spy after each test specStack Overflow