Sinon Spy永远不会被调用
问题描述:
我正在测试一个方法A,它根据条件调用具有不同参数的另一个方法B.所以我想监视B,以便我可以检查它是否被调用。但是间谍永远不会被召唤。Sinon Spy永远不会被调用
import parent from '../something.js'
describe('Testing A',() => {
it('should make proper calls to B',() => {
var spy = sinon.spy(parent, 'B')
parent.A()
expect(spy.calledOnce).to.be.true
})
})
和测试功能的也只是
export const A =() => {
B()
}
好像在测试中,B的间谍版本不会被调用,因为A直接呼叫B。如何让A的测试功能调用B的Sinon版本?
答
对我来说你的代码是不可测试的。您应该将待测组件视为黑盒子,而不是尝试使用其内部组件进行调整。在你的情况下,你试图窥探something.js模块内部的方法。
证B中的参数为A:
export const A = (B) => {
B();
}
在这种情况下,回调是很容易测试:
import parent from '../something.js'
describe('Testing A',() => {
it('should make proper calls to B',() => {
var B = sinon.spy();
parent.A(B);
expect(B.calledOnce).to.be.true;
})
})
+0
这似乎是最好的主意。谢谢。 – Roy
哪里B来到距离,这是内部的模块,它来自另一个模块? – Hosar
A和B都来自'父母' – Roy