javascript 茉莉花不会在每个测试规范后重置间谍

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11898069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:42:16  来源:igfitidea点击:

Jasmine does not reset spy after each test spec

javascriptunit-testingtestingjunitjasmine

提问by Sourabh

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.getLastExitLinkinternally use SN.Utils.

exitHistory.getLastExitLink内部使用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?

测试完成后,Jasmine 不会删除间谍对象 utils。在下一个测试套件中,我也可以看到相同的实用程序。每次测试完成后,有没有办法重置间谍对象?

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.

如果我为 utils 创建一个新对象,而不是创建 spy,行为是相同的。那么在这种情况下间谍和实际对象之间有什么区别。

Correct me if I am wrong.

如果我错了,请纠正我。

回答by elmendalerenda

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.com/pivotal/jasmine/wiki/Spies

这里描述了间谍:https: //github.com/pivotal/jasmine/wiki/Spies