Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a proper stub for custom subbed methods with callsFake #823

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/sinon/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@
invoke: function invoke(context, args) {
callCallback(this, args);

if (this.exception) {
if (this.fakeFn) {
return this.fakeFn.apply(context, args);
} else if (this.exception) {
throw this.exception;
} else if (typeof this.returnArgAt === "number") {
return args[this.returnArgAt];
Expand Down Expand Up @@ -176,6 +178,11 @@
);
},

callsFake: function callsFake(fn) {
this.fakeFn = fn;
return this;
},

callsArg: function callsArg(pos) {
if (typeof pos !== "number") {
throw new TypeError("argument index is not number");
Expand Down
2 changes: 1 addition & 1 deletion lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

if (func) {
if (typeof func === "function") {
wrapper = sinon.spy && sinon.spy.create ? sinon.spy.create(func) : func;
wrapper = sinon.stub && sinon.stub.create ? sinon.stub.create().callsFake(func) : func;
} else {
wrapper = func;
if (sinon.spy && sinon.spy.create) {
Expand Down
19 changes: 16 additions & 3 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@
}
},

".callsFake": {
setUp: function () {
this.stub = sinon.stub.create();
},

"calls fake function": function () {
var fake = sinon.stub.create();
this.stub.callsFake(fake);
this.stub(1, 2);
assert(fake.calledWith(1, 2));
}
},

".callsArg": {
setUp: function () {
this.stub = sinon.stub.create();
Expand Down Expand Up @@ -541,11 +554,11 @@
assert.isFunction(stub.throws);
},

"custom stubbed method should not be proper stub": function () {
"custom stubbed method should be proper stub": function () {
var stub = sinon.stub(this.object, "method", function () {});

refute.defined(stub.returns);
refute.defined(stub.throws);
assert.isFunction(stub.onCall);
assert.isFunction(stub.resetBehavior);
},

"stub should be spy": function () {
Expand Down