2017-10-06 46 views
0

我试图窥视传递到副作用-EY功能,它是由具有一个匿名函数容器接收一个最终PARAM间谍所有ARGS传递到功能组合物

(实际上是所有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

存根实际需要这样的:

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) 
}) 

这不会通过打电话到咖喱的功能,但如果你只是想检查被传递的参数,这不是必需的。

+0

这正是我所需要的。非常感谢! – tonywoode