【问题标题】:Using sinon fakeTimer with supertest lead to an error将 sinon fakeTimer 与 supertest 一起使用会导致错误
【发布时间】:2018-08-19 09:09:27
【问题描述】:

当我将 sinon fakeTimer 与 supertest 一起使用时,会导致错误Error: socket hang up。为什么以及如何解决它,请帮助?

【问题讨论】:

  • 可以分享代码吗?
  • “it”块的函数可以采用通常称为“done”的参数。然后,当您想要结束测试时调用它,done()。这可能会对您有所帮助。

标签: node.js automated-tests mocha.js sinon supertest


【解决方案1】:

这是一个非常古老的问题,我希望你找到了答案,但也许我的回答对其他人有帮助。

用 sinon 只伪造必要的“功能”。 useFakeTimers fake setIntervalsetImmediate js 功能默认,所以它会导致挂断。

设置useFakeTimers选项的toFake参数

const sandbox = createSandbox();
sandbox.useFakeTimers({
    now: currentDate.getTime(),
    toFake: ['Date']
  });
// ... tests
sandbox.restore();

https://sinonjs.org/releases/v6.2.0/fake-timers/

【讨论】:

    猜你喜欢
    • 2011-06-30
    • 2013-11-11
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2015-02-23
    相关资源
    最近更新 更多