【问题标题】:Sinon.test with typescript this is empty objectSinon.test with typescript 这是空对象
【发布时间】:2017-02-07 04:24:05
【问题描述】:

我正在尝试在 typescript 项目中使用 sinon,并尝试使用其沙盒功能。我已经在建议的庄园中完成了我的测试,并且我正在尝试按照文档调用this.stub(/<em>stuff</em>/)

不幸的是,当我这样做时,我收到以下错误TypeError: this.stub is not a function。作为测试,我在存根之前将this 打印到控制台,它是一个空对象。

以下是我尝试编写的测试失败的示例。非常感谢任何有关需要进行更改以使沙盒功能正常工作的建议。

import * as sinon from 'sinon'
import * as fs from 'fs'
describe("test",()=>
  it("raise error notification if location does not exist", sinon.test(()=>{
      this.stub(fs,"existsSync",(location: string)=> false)
      /* rest of test */
  }))
})

不使用沙盒功能并手动恢复存根确实可以参考。如果可能,尽量限制测试中的手动清理。

【问题讨论】:

  • share sinon import line else call like import * as sinon from 'sinon';
  • @Venkatraman 我更新了代码以包含我的两个包含语句以及描述块。

标签: typescript sinon


【解决方案1】:

今天早上找到了答案,任何人都发现了这篇文章。

问题是你不能使用新的 ES2015 箭头样式函数声明来访问正确的范围,这与 sinon.test。它来自那里 2.0 文档,但原因仍然转化为 1.0 分支

https://github.com/sinonjs/sinon-test/blob/master/README.md#usage

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多