Permalink
Browse files

eeek, uncovered a deep problem

  • Loading branch information...
1 parent 6cf673e commit 475aaab75a46233bd1f5bdfca37f3a9039ad052d @andykent andykent committed May 19, 2009
Showing with 21 additions and 6 deletions.
  1. +5 −3 lib/smoke.mock.js
  2. +16 −3 spec/mock_spec.js
View
@@ -35,9 +35,9 @@ Smoke.Mock = function(originalObj) {
var previousFunction = this[attr];
var mock = this;
this[attr] = function() {
- var result = expectation.run(arguments);
- if(result!=undefined) return result;
- return previousFunction!=undefined ? previousFunction.apply(mock,arguments) : undefined;
+ var expectationResult = expectation.run(arguments);
+ functionResult = previousFunction!=undefined ? previousFunction.apply(mock,arguments) : undefined;
+ return expectation.hasReturnValue ? expectationResult : functionResult
};
return expectation;
};
@@ -70,6 +70,7 @@ Smoke.Mock.Expectation = function(mock, attr) {
this.callCount = 0;
this.returnValue = undefined;
this.callerArgs = undefined;
+ this.hasReturnValue = false;
};
Smoke.Mock.Expectation.prototype = {
@@ -98,6 +99,7 @@ Smoke.Mock.Expectation.prototype = {
};
},
and_return: function(v){
+ this.hasReturnValue = true;
this.returnValue = v;
},
check: function(){
View
@@ -165,11 +165,24 @@ Screw.Unit(function() {
mockObj('a');
});
- it("should not call the original if a return value is specified", function() {
- var func = function() { throw("I should not be called") };
+ it("should call the original even if a return value is specified", function() {
+ var a = false
+ var func = function() { a = true };
var mockFunc = mock_function(func);
mockFunc.should_be_invoked().and_return('hello');
- expect(mockFunc()).to(equal, 'hello');
+ expect(mockFunc()).to(equal, 'hello');
+ expect(a).to(equal, true);
+ });
+
+ it("allows passing in a name for the function as a second argument to make error messages clearer", function() {
+ mock_function(foo, 'foo').should_be_invoked().exactly('once');
+ try {
+ Smoke.checkExpectations();
+ throw("exception");
+ } catch(e) {
+ Smoke.reset();
+ expect(e).to(equal, 'expected foo() to be called exactly 1 times but it got called 0 times');
+ }
});
});

0 comments on commit 475aaab

Please sign in to comment.