Skip to content

Commit

Permalink
defer evaluation of andCallFake for fallback after conditional stubbing
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice Jaglin committed Jan 15, 2014
1 parent 7417a74 commit a47f710
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/js/jasmine-stealth.coffee
Expand Up @@ -62,7 +62,7 @@
when "args" then jasmine.getEnv().equals_(stubbing.ifThis, jasmine.util.argsToArray(args))
when "context" then jasmine.getEnv().equals_(stubbing.ifThis,context)

priorStubbing = spy.plan()
priorPlan = spy.plan

spy.andCallFake ->
i = 0
Expand All @@ -74,7 +74,7 @@
else
return stubbing.thenThat
i++
priorStubbing
priorPlan.apply(spy, arguments)

jasmine.Spy::whenContext = (context) ->
spy = this
Expand Down
6 changes: 3 additions & 3 deletions dist/jasmine-stealth.js
Expand Up @@ -92,7 +92,7 @@
}
};
whatToDoWhenTheSpyGetsCalled = function(spy) {
var matchesStub, priorStubbing;
var matchesStub, priorPlan;
matchesStub = function(stubbing, args, context) {
switch (stubbing.type) {
case "args":
Expand All @@ -101,7 +101,7 @@
return jasmine.getEnv().equals_(stubbing.ifThis, context);
}
};
priorStubbing = spy.plan();
priorPlan = spy.plan;
return spy.andCallFake(function() {
var i, stubbing;
i = 0;
Expand All @@ -116,7 +116,7 @@
}
i++;
}
return priorStubbing;
return priorPlan.apply(spy, arguments);
});
};
jasmine.Spy.prototype.whenContext = function(context) {
Expand Down
23 changes: 22 additions & 1 deletion spec/jasmine-stealth-spec.coffee
Expand Up @@ -77,7 +77,7 @@
Given -> @spy.andReturn "football"
Given -> @spy.when("bored").thenReturn "baseball"

describe "it doesn't appear to invoke the spy", ->
describe "it doesn't appear to invoke the spy", ->
Then -> expect(@spy).not.toHaveBeenCalled()
Then -> @spy.callCount == 0
Then -> @spy.calls.length == 0
Expand All @@ -90,6 +90,27 @@
context "stubbing is satisfied", ->
Then -> @spy("bored") == "baseball"

context "default andCallFake plus some conditional stubbing", ->
Given -> @spy.andCallFake (s1,s2) -> s2
Given -> @spy.when("function").thenCallFake -> "football"
Given -> @spy.when("value").thenReturn "baseball"

describe "it doesn't appear to invoke the spy", ->
Then -> expect(@spy).not.toHaveBeenCalled()
Then -> @spy.callCount == 0
Then -> @spy.calls.length == 0
Then -> @spy.argsForCall.length == 0
Then -> expect(@spy.mostRecentCall).toEqual({})

context "default stubbing is satisfied", ->
Then -> @spy("cricket", "tennis") == "tennis"

context "conditional function stubbing is satisfied", ->
Then -> @spy("function") == "football"

context "conditional value stubbing is satisfied", ->
Then -> @spy("value") == "baseball"

describe "#whenContext", ->
Given -> @ctx = "A"
Given -> @spy = jasmine.createSpy().whenContext(@ctx).thenReturn("foo")
Expand Down

0 comments on commit a47f710

Please sign in to comment.