【问题标题】:How to test getCookie function with Jasmine and PhantomJS如何使用 Jasmine 和 PhantomJS 测试 getCookie 函数
【发布时间】:2015-05-12 08:44:19
【问题描述】:

我在窗口对象上有一个函数,如下所示:

window.getCookie = function(name){
   // Get cookie with name, uses document.cookie for this.
};

现在我想在我使用 Jasmine 和 PhantomJS 的单元测试中测试这个逻辑。测试如下:

it('should get a cookie with a specific name.', function () {
    // Setup
    document.cookie = 'Foo=Bar; expires=Thu, 01 Jan 1970 00:00:00 UTC';

    // Execute
    var result = window.getCookie('Foo');

    // Test
    expect(result).toBe('Bar');
});

事实证明,当代码在 PhantomJS 中执行时,document.cookie 总是 ''。所以在第 1 行设置 cookie 基本上是什么都不做。如果在第 2 行记录,则值为 ''。

我该如何解决这个问题?

【问题讨论】:

  • 我最终通过使用我自己的变量而不是 document.cookie 解决了这个问题。在现实世界中,我将该变量设置为等于 document.cookie 并且在我的测试中我可以覆盖它。这有点脏,所以我仍然愿意接受建议。

标签: unit-testing cookies jasmine phantomjs


【解决方案1】:

嗯,奇怪的是 document.cookie 是 '' 仅使用 PhantomJs。通常,当 cookie 过期时,它在 document.cookie 中不再可用。

【讨论】:

  • 请不要将答案用作评论
猜你喜欢
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 2018-02-24
  • 1970-01-01
相关资源
最近更新 更多