【发布时间】:2018-09-24 11:14:54
【问题描述】:
我想监视一个在需要文件时立即执行的函数。在下面的示例中,我想监视 bar。我有以下文件。
code.ts
import {bar} from 'third-party-lib';
const foo = bar()
test.ts
import * as thirdParty from 'third-party-lib';
describe('test', () => {
let barStub: SinonStub;
beforeEach(() => {
barStub = sinon.stub(thridParty, 'bar')
})
it('should work', () => {
assert.isTrue(bar.calledOnce)
})
}
存根不起作用。我认为这是一个时间问题。 Bar 在执行后会被存根。如果我将第一行包装在一个函数中并在我的测试中执行该函数,则上面的示例有效。但这不是我想要的。有人知道如何存根这些方法吗?
【问题讨论】:
-
你使用什么测试框架? (看起来像
Jest,对吗?)
标签: node.js unit-testing mocking sinon