简体   繁体   English

测试 function 以检查 Qunit 是否启用了 cookies

[英]Testing function which checks if cookies are enabled with Qunit

I have the following function which checks if cookies are enabled in a user's browser:我有以下 function 检查是否在用户的浏览器中启用了 cookies:

CookiesEnabled: function() {
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled) {
        document.cookie = "test_cookie";
        cookieEnabled = document.cookie.indexOf("test_cookie") != -1;
    }

    return cookieEnabled;
},

I want to unit test that this works using Sinon/Qunit but am unsure of how to approach this properly as I am not very experienced using Sinon or QUnit.我想对使用 Sinon/QUnit 进行单元测试,但我不确定如何正确处理这个问题,因为我对使用 Sinon 或 QUnit 不是很有经验。 Here is my attempt at putting together a test:这是我尝试进行测试的尝试:

QUnit.test("CookiesEnabled - returns true if user has enabled cookies in their browser", function (assert) {
    sinon.stub(CookieHelper, "CookiesEnabled")
         .callsFake(function () {});

    var result = CookieHelper.CookiesEnabled();

    Assert.Equal(result, true);

    CookieHelper.CookiesEnabled.restore();
});

Please can anyone point me in the right direction for unit testing this method using Qunit and Sinon?请谁能指出我使用 Qunit 和 Sinon 对这种方法进行单元测试的正确方向? Thank you.谢谢你。

UPDATED ATTEMPT:更新的尝试:

QUnit.test("CookiesEnabled - returns true if user has enabled cookies in their browser", function (assert) {
    sinon.stub(navigator, "cookieEnabled").callsFake(function () {
        return true;
    });

    var result = CookieHelper.CookiesEnabled();

    assert.equal(result, true);

});

As mentioned, window.navigator is readonly.如前所述, window.navigator是只读的。 There are some things you could try to fake out the browser, but really, you should just inject the properties you're interested in for the function to use.有些事情你可以尝试伪造浏览器,但实际上,你应该只注入你感兴趣的属性供 function 使用。 This way you can override the browser setting or test it.这样您就可以覆盖浏览器设置或对其进行测试。 Notice the addition of two arguments (since you also need to bypass your double-checking of cookie setting).请注意添加了两个 arguments(因为您还需要绕过对 cookie 设置的双重检查)。

The additional flag argument is a boolean that essentially tells the function what the value of cookieEnabled should be (versus using navigator.cookieEnabled ).附加的flag参数是 boolean,它基本上告诉 function cookieEnabled的值应该是什么(与使用navigator.cookieEnabled相比)。 However, if that parameter is missing, the code will use the browser setting.但是,如果缺少该参数,代码将使用浏览器设置。 In other words, it's a way to "inject" the value you want, very useful in testing.换句话说,这是一种“注入”你想要的值的方法,在测试中非常有用。 This isn't ideal, but not much else you can without a lot more work and mocking (using a headless browser with cookies disabled).这并不理想,但如果没有更多的工作和 mocking(使用禁用 cookies 的无头浏览器),您就无法做到。

Here's a jsfiddle of this working: https://jsfiddle.net/e5mtpboy/这是这个工作的一个jsfiddle: https://jsfiddle.net/e5mtpboy/

CookiesEnabled: function(flag, bypassTest) {
    var cookieEnabled = typeof(flag) === 'boolean' ? flag : navigator.cookieEnabled;
    if (!cookieEnabled && !bypassTest) {
        document.cookie = "test_cookie";
        cookieEnabled = document.cookie.indexOf("test_cookie") != -1;
    }

    return cookieEnabled;
}

Then in your tests:然后在你的测试中:

QUnit.test("CookiesEnabled - returns true if user has enabled cookies in their browser", function (assert) {
    var result = CookieHelper.CookiesEnabled(true);
    assert.equal(result, true);
});

QUnit.test("CookiesEnabled - returns true if user has disabled cookies, but we can still set one", function (assert) {
    var result = CookieHelper.CookiesEnabled(false);
    assert.equal(result, true);
});

QUnit.test("CookiesEnabled - returns false if browser does not support cookies", function (assert) {
    var result = CookieHelper.CookiesEnabled(false, true);
    assert.equal(result, false);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM