问题描述
有什么区别
-
stub.yield([arg1,arg2,...])
-
spy.yields([arg1,arg2,...])
-
stub.callsArg(index)
stub.yield([arg1, arg2, ...])
spy.yields([arg1, arg2, ...])
stub.callsArg(index)
存根库?
stub.yield()
是我唯一能够掌握的:
stub.yield()
is the only one that I've been able to grasp:
stub = sinon.stub(API, 'call_remote');
callback = sinon.spy();
API.call_remote('help', callback);
@stub.yield( "solution!" );
@stub.calledOnce.should.be.true;
@callback.calledOnce.should.be.true;
@callback.args[0][0].should.eql( "solution!" );
使用should.js进行测试时,所有断言都会通过。
As tested with should.js, would have all assertions pass.
是否有类似的测试模式 stub.yields()
和 stub.callsArg(index)
?
Are there similar test patterns for stub.yields()
and stub.callsArg(index)
?
文档没有提供任何示例来澄清这两种方法,但我对它们感到好奇。
The documentation does not provide any examples to clarify these other two methods but I am curious about them.
推荐答案
我认为文档中概述的方法如下:
I believe the methods, as outlined in the documentation, are as follows:
-
spy.yield
-
stub.yields
-
stub.callsArg
spy.yield
stub.yields
stub.callsArg
<$ c $之间的主要区别c> yield 和 callsArg
可以在sinon的文档中找到产量:
The main difference between yields
and callsArg
can be found in sinon's documentation for yields:
收益
将调用 fir st 函数参数,它遇到您提供给它的任何可选参数。 callsArg
将尝试在该调用的参数
对象中的给定索引处调用函数参数,并且不传递任何参数(您可以使用 callArgWith
来表示该行为)。
yields
will call the first function argument it encounters with any optional arguments you provide to it. callsArg
will attempt to invoke the function argument at the given index within that call's arguments
object, and does not pass any arguments to it (you can use callArgWith
for that behavior).
spy.yield
非常类似于 stub.yields
除了它是间谍API的一部分,它调用传递给它的所有回调。
spy.yield
is very similar to stub.yields
except it is part of the spy API and it calls all callbacks passed to it.
以下是一些展示差异的例子(如果示例有点人为,请原谅我):
Here's some examples demonstrating the differences (forgive me if the examples are a bit contrived):
收益率:
var fn = sinon.expectation.create().withArgs(1, 2);
var stub = sinon.stub().yields(1, 2);
stub(fn);
fn.verify();
CallsArg:
var f1 = sinon.expectation.create().never();
var f2 = sinon.expectation.create().once();
var stub = sinon.stub().callsArg(1);
stub(f1, f2);
f1.verify();
f2.verify();
收益率:
var f1 = sinon.expectation.create().once();
var f2 = sinon.expectation.create().once();
var stub = sinon.stub().yield();
stub(f1, f2);
f1.verify();
f2.verify();
这篇关于理解Sinon.js的yield(),yield()和callsArg()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!