javascript 理解Sinon.js的yield()、yields()和callsArg()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16003218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:53:45  来源:igfitidea点击:

Understanding Sinon.js's yield(), yields(), and callsArg()

javascriptsinon

提问by TimeEmit

What is the difference between

之间有什么区别

  • stub.yield([arg1, arg2, ...])
  • spy.yields([arg1, arg2, ...])
  • stub.callsArg(index)
  • stub.yield([arg1, arg2, ...])
  • spy.yields([arg1, arg2, ...])
  • stub.callsArg(index)

in the Sinon.jsstub library?

Sinon.js存根库中?

stub.yield()is the only one that I've been able to grasp:

stub.yield()是我唯一能够掌握的:

  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!" );

As tested with should.js, would have all assertions pass.

使用 should.js 进行测试时,所有断言都会通过。

Are there similar test patterns for stub.yields()and stub.callsArg(index)?

是否有类似的测试模式stub.yields()stub.callsArg(index)

The documentation does not provide any examples to clarify these other two methods but I am curious about them.

该文档没有提供任何示例来阐明这其他两种方法,但我对它们很好奇。

回答by Travis Kaufman

I believe the methods, as outlined in the documentation, are as follows:

我相信文档中概述的方法如下:

  • spy.yield
  • stub.yields
  • stub.callsArg
  • spy.yield
  • stub.yields
  • stub.callsArg

The main difference between yieldsand callsArgcan be found in sinon's documentation for yields:

yields和之间的主要区别callsArg可以在 sinon 的产量文档中找到:

If a method accepts more than one callback, you need to use callsArg to have the stub invoke other callbacks than the first one.

如果一个方法接受多个回调,则需要使用 callArg 使存根调用除第一个回调之外的其他回调。

yieldswill call the firstfunction argument it encounters with any optional arguments you provide to it. callsArgwill attempt to invoke the function argument at the given indexwithin that call's argumentsobject, and does not pass any arguments to it (you can use callArgWithfor that behavior).

yields将使用您提供给它的任何可选参数调用它遇到的第一个函数参数。callsArg将尝试 在该调用的对象内的给定索引处调用函数参数arguments,并且不向它传递任何参数(您可以callArgWith用于该行为)。

spy.yieldis very similar to stub.yieldsexcept it is part of the spy API and it calls all callbacks passed to it.

spy.yield非常类似于stub.yields除了它是间谍 API 的一部分并且它调用传递给它的所有回调。

Here's some examples demonstrating the differences (forgive me if the examples are a bit contrived):

以下是一些演示差异的示例(如果示例有点做作,请原谅我):

Yields:

产量:

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();

Yield:

屈服:

var f1 = sinon.expectation.create().once();
var f2 = sinon.expectation.create().once();
var stub = sinon.stub().yield();
stub(f1, f2);
f1.verify();
f2.verify();