测试在qunit中使用文档属性的函数

时间:2015-01-06 17:07:36

标签: javascript qunit sinon

我有一个这样的功能,我想写一个单元测试:

function A () {
    var x = document.referrer;
    //Do something with 'a' 
}

我想写一个单元测试,来测试这个功能。我正在使用QUnit和sinon.js进行存根和模拟。有没有办法让我模拟文档对象,以便我可以为引用者提供一个值并测试我的逻辑?我尝试在document.referrer函数中为setup分配值,但这不起作用

更新

这是我正在尝试的测试:

module("Verify Referrer", {
    setup : function () {
        this.documentMock = sinon.mock(document);
        this.documentMock.referrer = "http://www.test.com";
    }, 
    teardown : function () {
        this.documentMock.restore();
    }
});

test("UrlFilter test url in white list", function() {
    equal(document.referrer, "http://www.test.com");
});

我也尝试过使用stub

0 个答案:

没有答案
相关问题