Verifying function call and inspecting arguments using sinon spies

前端 未结 3 2149
[愿得一人]
[愿得一人] 2020-12-16 11:08

I would like to verify that bar() is called inside foo() from my unit test.

I figured that Sinon spies might be suitable, but I don\'t know

3条回答
  •  不知归路
    2020-12-16 11:36

    I agree with Adrian in saying that you probably wanted to spy on bar instead.

    var barSpy = sinon.spy(bar);
    

    Then to check that it was called once

    assert(barSpy.calledOnce);
    

    Just called at all

    assert(barSpy.called)
    

    Called x amount of times

    assert.equal(barSpy.callCount, x);
    

    If you want to extract the arguments from the first call of the spy:

    var args = barSpy.getCalls()[0].args
    

    Then you can do what you want with those arguments.

提交回复
热议问题