【问题标题】:spy on all args passed to a function composition监视传递给函数组合的所有参数
【发布时间】:2017-10-06 10:22:51
【问题描述】:

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

(其实我想把它存根,但间谍只是一个开始)

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?

【问题讨论】:

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


    【解决方案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 函数,但如果您只想检查传递的参数,则不需要。

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多