Skip to content

Commit

Permalink
Add tests for call return value tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Dec 8, 2013
1 parent 3b52d01 commit b1d4ab0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions spec/core/SpySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ describe('Spies', function () {

expect(trackSpy.calls.mostRecent().args[0].object).toEqual(contextObject);
});

it("tracks the return value of calls", function () {
var spy = j$.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
var trackSpy = spyOn(spy.calls, "track");

spy.and.returnValue("return value");
spy();

expect(trackSpy.calls.mostRecent().args[0].returnValue).toEqual("return value");
});
});

describe("createSpyObj", function() {
Expand Down
13 changes: 11 additions & 2 deletions spec/core/integration/EnvSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ describe("Env integration", function() {
var env = new j$.Env();

var originalFunctionWasCalled = false;
var subject = { spiedFunc: function() { originalFunctionWasCalled = true; } };
var subject = {
spiedFunc: function() {
originalFunctionWasCalled = true;
return "original result";
}
};

var spy = env.spyOn(subject, 'spiedFunc');
var spy = env.spyOn(subject, 'spiedFunc').and.returnValue("stubbed result");

expect(subject.spiedFunc).toEqual(spy);

Expand All @@ -220,11 +225,15 @@ describe("Env integration", function() {
expect(subject.spiedFunc.calls.count()).toEqual(1);
expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['foo']);
expect(subject.spiedFunc.calls.mostRecent().object).toEqual(subject);
expect(subject.spiedFunc.calls.mostRecent().returnValue).toEqual("stubbed result");
expect(originalFunctionWasCalled).toEqual(false);

subject.spiedFunc.and.callThrough();
subject.spiedFunc('bar');
expect(subject.spiedFunc.calls.count()).toEqual(2);
expect(subject.spiedFunc.calls.mostRecent().args).toEqual(['bar']);
expect(subject.spiedFunc.calls.mostRecent().returnValue).toEqual("original result");
expect(originalFunctionWasCalled).toEqual(true);
});

it("Mock clock can be installed and used in tests", function(done) {
Expand Down

0 comments on commit b1d4ab0

Please sign in to comment.