fil*_*lur 22 javascript unit-testing sinon
我想验证从我的单元测试中bar()调用内部foo().
我认为Sinon间谍可能是合适的,但我不知道如何使用它们.
有没有办法检查方法是否被调用?甚至可能提取调用中使用的参数bar()?
var spy = sinon.spy(foo);
function foo(){
bar(1,2,3);
}
function bar(){ }
foo();
// what to do with the spy?
Run Code Online (Sandbox Code Playgroud)
pht*_*ier 30
在你的情况下,你试图看看是否bar被调用,所以你想要间谍bar而不是foo.
如文档中所述:
function bar(x,y) {
console.debug(x, y);
}
function foo(z) {
bar(z, z+1);
}
// Spy on the function "bar" of the global object.
var spy = sinon.spy(window, "bar");
// Now, the "bar" function has been replaced by a "Spy" object
// (so this is not necessarily what you want to do)
foo(1);
bar.getCall(0).args => should be [1,2]
Run Code Online (Sandbox Code Playgroud)
现在,监视函数内部强烈地将你对"foo"的测试与它的实现结合起来,所以你将陷入通常的"模仿者与经典"辩论之中.
小智 9
我同意阿德里安的说法,你可能想偷窥吧.
var barSpy = sinon.spy(bar);
Run Code Online (Sandbox Code Playgroud)
然后检查它是否被调用过一次
assert(barSpy.calledOnce);
Run Code Online (Sandbox Code Playgroud)
刚刚打过电话
assert(barSpy.called)
Run Code Online (Sandbox Code Playgroud)
被称为x次
assert.equal(barSpy.callCount, x);
Run Code Online (Sandbox Code Playgroud)
如果你想从第一次调用spy中提取参数:
var args = barSpy.getCalls()[0].args
Run Code Online (Sandbox Code Playgroud)
然后你可以用这些参数做你想做的事.
| 归档时间: |
|
| 查看次数: |
35644 次 |
| 最近记录: |