sinonjs / sinon

Test spies, stubs and mocks for JavaScript.
https://sinonjs.org/
Other
9.66k stars 771 forks source link

firstCall, secondCall [...] not accessible for throwing calls. #260

Closed pgayvallet closed 10 years ago

pgayvallet commented 11 years ago

Short code is better than long speech :

// this test pass
test("calls should be accessible", function() {

    var stub = this.stub();
    stub.returns("ahah");
    stub.withArgs("throw").returns("hello");

    stub("foo");
    stub("throw");

    equal(stub.callCount, 2);
    ok(stub.firstCall != null);
    ok(stub.secondCall != null);

})

// this test last asserts fails.
test("throwing calls should be accessible", function() {

    var stub = this.stub();
    stub.returns("ahah");
    stub.withArgs("throw").throws("AnyException");

    stub("foo");
    try {
        stub("throw");
    } catch(e) {};

    equal(stub.callCount, 2);
    ok(stub.firstCall != null);
    ok(stub.secondCall != null); // <-- this fail.

})

looking at the code, this is simply because the 'createCallProperties' call in invoke and withArgs is called too late in case of throwing calls.

pgayvallet commented 11 years ago

Note : using sinon-js 1.6.0 . This example uses sinon-qunit.

mantoni commented 11 years ago

Since you already have a failing test case and know how to fix it in the code, you could provide a patch ;-)