监视传递给函数组合的所有参数

spy on all args passed to a function composition

我试图监视传递给副作用函数的所有参数,该函数由接收最终参数的匿名函数容器组成

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

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

实际上需要存根:

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 函数,但如果您只想检查传递的参数,则不需要这样做。