理解Sinon.js的yield(),yield()和callsArg() [英] Understanding Sinon.js's yield(), yields(), and callsArg()

查看:266
本文介绍了理解Sinon.js的yield(),yield()和callsArg()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别


  • stub.yield([arg1,arg2,...])

  • spy.yields([arg1,arg2,...])

  • stub.callsArg(index)

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

Sinon.js 存根库?

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:


如果一个方法接受多个回调,你需要使用callsArg让stub调用除第一个之外的其他回调。

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

收益将调用 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆