Verifying function call and inspecting arguments using sinon spies

前端 未结 3 2114
[愿得一人]
[愿得一人] 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:22

    In your case, you are trying to see if bar was called, so you want to spy bar rather than foo.

    As described in the doc :

    function bar(x,y) {
      console.debug(x, y);
    }
    function foo(z) {
      bar(z, z+1);
    }
    // Spy on the function "bar" of the global object.
    var spy = sinon.spy(window, "bar");
    
    // Now, the "bar" function has been replaced by a "Spy" object
    // (so this is not necessarily what you want to do) 
    
    foo(1);
    
    bar.getCall(0).args => should be [1,2]
    

    Now, spying on the internals of the function strongly couples your test of "foo" to its implementation, so you'll fall into the usual "mockist vs classical" debate.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-16 11:38

    Shouldn't you be spying on bar, rather than foo?

    var spy = sinon.spy(bar)
    

    Call foo:

    foo()
    

    Check bar was called:

    console.log(spy.calledOnce)
    
    0 讨论(0)
提交回复
热议问题