Permalink
Browse files

Refactored Smoke.MockFunction to not require the mockFunction propert…

…y to bet set. Added a couple additional tests
  • Loading branch information...
kristopher committed Jun 8, 2009
1 parent 1f6e833 commit d34586dd8d2700fbc5b76712125868de894d5304
Showing with 24 additions and 7 deletions.
  1. +12 −7 lib/smoke.mock.js
  2. +12 −0 spec/mock_spec.js
View
@@ -49,13 +49,18 @@ Smoke.Mock = function(originalObj) {
};
Smoke.MockFunction = function(originalFunction, name) {
- var name = name || 'anonymous_function';
- var originalFunction = originalFunction || function() {};
- var scope = function() { return scope.mockFunction.apply(this,arguments) };
- scope[name] = originalFunction;
- scope.mockFunction = function() { return scope[name].apply(this,arguments); };
- var mock = Smoke.Mock(scope);
- mock.should_be_invoked = function() { return mock.should_receive(name) };
+ name = name || 'anonymous_function';
+ var mock = Smoke.Mock(function() {
+ var return_value = arguments.callee[name].apply(this, arguments);
+ if (return_value === undefined) {
+ return_value = (originalFunction || new Function()).apply(this, arguments)
+ }
+ return return_value;
+ });
+ mock[name] = (originalFunction || new Function());
+ mock.should_be_invoked = function() {
+ return this.should_receive(name);
+ }
return mock;
};
View
@@ -164,6 +164,18 @@ Screw.Unit(function() {
mockObj('a');
mockObj('a');
});
+
+ it("should allow a return value to be set", function() {
+ mockObj.should_be_invoked().and_return('bar');
+ expect(mockObj('foo')).to(equal, 'bar');
+ });
+
+ it("should allow multiple return values to be set through the argument matchers", function() {
+ mockObj.should_be_invoked().with_arguments('foo').and_return('bar');
+ mockObj.should_be_invoked().with_arguments('bar').and_return('foo');
+ expect(mockObj('foo')).to(equal, 'bar');
+ expect(mockObj('bar')).to(equal, 'foo');
+ });
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');

0 comments on commit d34586d

Please sign in to comment.