diff --git a/lib/sinon/behavior.js b/lib/sinon/behavior.js index 15cb524e6..567b6eca6 100644 --- a/lib/sinon/behavior.js +++ b/lib/sinon/behavior.js @@ -170,7 +170,8 @@ var proto = { } else if (this.reject) { return (this.promiseLibrary || Promise).reject(this.returnValue); } else if (this.callsThrough) { - return this.stub.wrappedMethod.apply(context, args); + var wrappedMethod = this.effectiveWrappedMethod(); + return wrappedMethod.apply(context, args); } else if (typeof this.returnValue !== "undefined") { return this.returnValue; } else if (typeof this.callArgAt === "number") { @@ -180,6 +181,15 @@ var proto = { return this.returnValue; }, + effectiveWrappedMethod: function effectiveWrappedMethod() { + for (var stubb = this.stub; stubb; stubb = stubb.parent) { + if (stubb.wrappedMethod) { + return stubb.wrappedMethod; + } + } + throw new Error("Unable to find wrapped method"); + }, + onCall: function onCall(index) { return this.stub.onCall(index); }, diff --git a/test/issues/issues-test.js b/test/issues/issues-test.js index 9722298c0..48a59da15 100644 --- a/test/issues/issues-test.js +++ b/test/issues/issues-test.js @@ -600,6 +600,26 @@ describe("issues", function() { }); }); + describe("#1964", function() { + it("should allow callThrough on a withArgs fake", function() { + var calledThrough = false; + var obj = { + method: function() { + calledThrough = true; + } + }; + + var baseStub = sinon.stub(obj, "method"); + baseStub.throws("Should always hit the withArgs fake"); + var argsStub = baseStub.withArgs("foo").callThrough(); + + obj.method("foo"); + + sinon.assert.calledOnce(argsStub); + assert.isTrue(calledThrough); + }); + }); + describe("#2016", function() { function Foo() { return;