Skip to content

Commit 6cbde9b

Browse files
authored
fix throws().callsFake() precedence (#2497)
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.
1 parent 45be60f commit 6cbde9b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/sinon/default-behaviors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function throwsException(fake, error, message) {
3131
var defaultBehaviors = {
3232
callsFake: function callsFake(fake, fn) {
3333
fake.fakeFn = fn;
34+
fake.exception = undefined;
35+
fake.exceptionCreator = undefined;
3436
},
3537

3638
callsArg: function callsArg(fake, index) {

test/stub-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,28 @@ describe("stub", function () {
14071407
refute(fakeFn.called);
14081408
assert(returned === 3);
14091409
});
1410+
1411+
it("supersedes previous throws(error)", function () {
1412+
var fakeFn = createStub().returns(5);
1413+
this.stub = createStub(this.object, "method");
1414+
1415+
this.stub.throws(new Error("error")).callsFake(fakeFn);
1416+
var returned = this.object.method(1, 2);
1417+
1418+
assert(fakeFn.called);
1419+
assert(returned === 5);
1420+
});
1421+
1422+
it("supersedes previous throws()", function () {
1423+
var fakeFn = createStub().returns(5);
1424+
this.stub = createStub(this.object, "method");
1425+
1426+
this.stub.throws().callsFake(fakeFn);
1427+
var returned = this.object.method(1, 2);
1428+
1429+
assert(fakeFn.called);
1430+
assert(returned === 5);
1431+
});
14101432
});
14111433

14121434
describe(".objectMethod", function () {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy