茉莉花钟不使用cookie过期

时间:2018-02-08 14:10:04

标签: javascript unit-testing cookies jasmine

我刚刚在Jasmine写了一个失败的规范,我不明白为什么。我正在尝试编写一个有过期日期的cookie,然后在到期后检查它是否仍然可用。目前看起来它没有被删除。这是我的代码:

 beforeAll(function() {
    jasmine.clock().install();
 });
 it('succesfully sets a cookie with time', function() {
        var d = new Date();
        d.setTime(d.getTime() + 1*60*1000); // in milliseconds
        document.cookie = 'testCookieWithTime=helloWorld;path=/;max-age='+1*60+';expires='+d.toUTCString()+';';
        jasmine.clock().mockDate();
        jasmine.clock().tick(650000); //millis

        var v = document.cookie.match('(^|;) ?' + 'testCookieWithTime' + '=([^;]*)(;|$)');

        expect(v[2]).not.toBeDefined();
    });
 afterAll(function() {
    jasmine.clock().uninstall();
 });

不幸的是,这是我的测试结果:

Summary (1 tests failed)
X Cookies methods specs succesfully sets a cookie with time
Expected 'helloWorld' not to be defined.

为了测试目的,我将cookie过期时间设置为1分钟,并用数字替换了一些变量(因为我的代码被拆分为方法)。可能它有点小,但我找不到它。非常感谢帮助。

对于它的价值,获取和设置cookie的代码基于:https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/

1 个答案:

答案 0 :(得分:0)

找到解决方案:

it('succesfully sets a cookie with time', function() {
    cookie_set_method(); 
    jasmine.clock().install();
    jasmine.clock().tick(4000); //millis
    expect(Cookie_get_method()).toEqual(false); //does not exist
    jasmine.clock().uninstall();
});

我不完全确定发生了什么,但我确实混淆了一些事情。