间接传递给函数组合的所有args

时间:2017-10-06 10:22:52

标签: javascript node.js unit-testing sinon partial-application

我正在试图监视传递给side-effect-ey函数的所有params,它由一个接收最终参数的匿名函数容器组成

(实际上我想要存根,但间谍活动将是一个开始)

classA.js

const classB = require(`classB.js`)

const doWork = ( a, b, c, d, e ) => {
  //do things with a, b, c, d, e to make x, y, z…
  return classB.thingToSpyOn(a, b, c, x, y)(z) //<=note curry here
}

ClassA.spec.js

const classA = require(`classA.js`)
const classB = require(`classB.js`)

describe(`doWork`, () => {
  sinon.spy(classB, 'thingToSpyOn' ) 

  classA.doWork( “foo”, “bar”, “baz”, “bing”, “boing”)

  //now i can get the spy to tell me what it received as a, b, c, x, y
  console.log(classB.thingToSpyOn.args)
  ...

但如何记录收到的内容为z

1 个答案:

答案 0 :(得分:1)

实际上需要存根:

describe(`doWork`, () => {
  let zSpy = sinon.spy();
  sinon.stub(classB, 'thingToSpyOn' ).returns(zSpy);

  classA.doWork( 'foo', 'bar', 'baz', 'bing', 'boing' )

  console.log(classB.thingToSpyOn.args)
  console.log(zSpy.args)
})

这不会调用curried函数,但如果你只想检查传递的参数,则不需要这样做。