Skip to content

Commit

Permalink
Fix sinonjs#1487: incorrect withArgs().returnValue
Browse files Browse the repository at this point in the history
  • Loading branch information
takasmiley authored and Franck Romano committed Oct 1, 2019
1 parent 40e24f1 commit b55a068
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/sinon/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ var spyApi = {

// Make return value and exception available in the calls:
createCallProperties.call(this);
matchings.forEach(function (matching) {
createCallProperties.call(matching);
});

if (exception !== undefined) {
throw exception;
Expand Down
34 changes: 34 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@ describe("stub", function () {
refute.isNull(stub.withArgs(1).firstCall);
});

describe("should work firstCall and lastCall", function () {
var testComponentA = function () { return { render: function () { return "test a"; } }; };
var testComponentB = function () { return { render: function () { return "test b"; } }; };
var inject;

beforeEach(function () {
var fakeComponent = function (variant) {
return createStub().returns({
render: createStub().returns("fake component " + variant)
});
};
inject = createStub().throws("Nothing set");
inject.withArgs(testComponentA).returns(fakeComponent("a"));
inject.withArgs(testComponentB).returns(fakeComponent("b"));
});

it("returnValues", function () {
var config = { option: "a" };
var component = inject(testComponentA)(config);

assert.isTrue(inject.calledWith(testComponentA));
assert.isFalse(inject.calledWith(testComponentB));

assert.isFunction(component.render);
assert.equals(component.render(), "fake component a");

assert.isTrue(inject.withArgs(testComponentA).returnValues[0].calledWith(config));
assert.isTrue(inject.withArgs(testComponentA).getCall(0).returnValue.calledWith(config));

assert.isTrue(inject.withArgs(testComponentA).firstCall.returnValue.calledWith(config));
assert.isTrue(inject.withArgs(testComponentA).lastCall.returnValue.calledWith(config));
});
});

describe(".returns", function () {
it("returns specified value", function () {
var stub = createStub.create();
Expand Down

0 comments on commit b55a068

Please sign in to comment.