Skip to content

Commit

Permalink
fix throws().callsFake() precedence (#2497)
Browse files Browse the repository at this point in the history
This makes sure an unconditional `callsFake()` invoked on the same stub that was previously setup to throw will overwrite the previous behavior. This aligns it with the other behaviors.
  • Loading branch information
ziluvatar committed Mar 12, 2023
1 parent 45be60f commit 6cbde9b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/sinon/default-behaviors.js
Expand Up @@ -31,6 +31,8 @@ function throwsException(fake, error, message) {
var defaultBehaviors = {
callsFake: function callsFake(fake, fn) {
fake.fakeFn = fn;
fake.exception = undefined;
fake.exceptionCreator = undefined;
},

callsArg: function callsArg(fake, index) {
Expand Down
22 changes: 22 additions & 0 deletions test/stub-test.js
Expand Up @@ -1407,6 +1407,28 @@ describe("stub", function () {
refute(fakeFn.called);
assert(returned === 3);
});

it("supersedes previous throws(error)", function () {
var fakeFn = createStub().returns(5);
this.stub = createStub(this.object, "method");

this.stub.throws(new Error("error")).callsFake(fakeFn);
var returned = this.object.method(1, 2);

assert(fakeFn.called);
assert(returned === 5);
});

it("supersedes previous throws()", function () {
var fakeFn = createStub().returns(5);
this.stub = createStub(this.object, "method");

this.stub.throws().callsFake(fakeFn);
var returned = this.object.method(1, 2);

assert(fakeFn.called);
assert(returned === 5);
});
});

describe(".objectMethod", function () {
Expand Down

0 comments on commit 6cbde9b

Please sign in to comment.