Permalink
Browse files

Mocked objects put their original behavior back after each test is ran.

  • Loading branch information...
1 parent b95e8a7 commit e75979515439343a6e62e106dbebc2aebbacbe9b Larry Karnowski committed Jul 17, 2009
Showing with 58 additions and 5 deletions.
  1. +20 −3 lib/smoke.mock.js
  2. +38 −2 spec/mock_spec.js
View
23 lib/smoke.mock.js
@@ -10,6 +10,8 @@ Smoke.failed = function(mock, message){
// Some helpers
Smoke.reset = function(){
+ Smoke.mocks = Smoke.mocks || [];
+ for(var i=0; i<Smoke.mocks.length; i++) Smoke.mocks[i].resetMocks();
Smoke.mocks = [];
Smoke.passCount = 0;
Smoke.failCount = 0;
@@ -20,21 +22,23 @@ Smoke.checkExpectations = function(){
for(var i=0; i<Smoke.mocks.length; i++) Smoke.mocks[i].checkExpectations();
};
-// Don't play beyond here unless you know what you're doing
Smoke.Mock = function(originalObj) {
var obj = originalObj || {} ;
obj._expectations = {};
+ obj._valuesBeforeMocking = {};
+
obj.stub = function(attr){
return new Smoke.Stub(this, attr);
};
obj.should_receive = function(attr){
- var expectation = new Smoke.Mock.Expectation(this, attr)
+ var expectation = new Smoke.Mock.Expectation(this, attr);
this._expectations[attr] = (this._expectations[attr] || []).concat([expectation]);
+ this._valuesBeforeMocking[attr] = this[attr];
if(this._expectations[attr].length == 1) {
this[attr] = Smoke.Mock.Expectation.stub(this, attr);
}
- return expectation;
+ return expectation;
};
obj.checkExpectations = function(){
@@ -44,6 +48,19 @@ Smoke.Mock = function(originalObj) {
};
};
+ obj.resetMocks = function(){
+ for(var attr in this._valuesBeforeMocking) {
+ this[attr] = this._valuesBeforeMocking[attr];
+ }
+
+ delete this._valuesBeforeMocking;
+ delete this._expectations;
+ delete this.stub;
+ delete this.should_receive;
+ delete this.checkExpectations;
+ delete this.resetMocks;
+ };
+
Smoke.mocks.push(obj);
return obj;
};
View
40 spec/mock_spec.js
@@ -128,11 +128,11 @@ Screw.Unit(function() {
});
});
- describe("idempotency of mocks on global variables", function(){
+ describe("proper teardown of mocks on global variables", function(){
var SomeGlobal = { say: "hello", shout: function() { return this.say.toUpperCase(); } };
it("when mocked in one test...", function(){
- mock(SomeGlobal).should_receive("shout").exactly(1, "times").and_return("some string");
+ mock(SomeGlobal).should_receive("shout").and_return("some string");
expect(SomeGlobal.shout()).to(equal, "some string");
});
@@ -141,6 +141,42 @@ Screw.Unit(function() {
});
});
+ describe("reseting mocks", function(){
+ it("should remove all mocking data from an object", function(){
+ var obj = { say: "hello", shout: function() { return this.say.toUpperCase(); } };
+ mock(obj).should_receive("shout").and_return("some string");
+
+ expect(obj._valuesBeforeMocking).to_not(equal, null);
+ expect(obj._expectations).to_not(equal, null);
+ expect(obj.stub).to_not(equal, null);
+ expect(obj.should_receive).to_not(equal, null);
+ expect(obj.checkExpectations).to_not(equal, null);
+ expect(obj.resetMocks).to_not(equal, null);
+
+ obj.resetMocks();
+ Smoke.mocks = [];
+
+ expect(obj._valuesBeforeMocking).to(equal, null);
+ expect(obj._expectations).to(equal, null);
+ expect(obj.stub).to(equal, null);
+ expect(obj.should_receive).to(equal, null);
+ expect(obj.checkExpectations).to(equal, null);
+ expect(obj.resetMocks).to(equal, null);
+ });
+
+ it("should replace the original functionality to the object", function(){
+ var obj = { say: "hello", shout: function() { return this.say.toUpperCase(); } };
+ mock(obj).should_receive("shout").and_return("some string");
+ expect(obj.shout()).to(equal, "some string");
+
+ obj.resetMocks();
+ Smoke.mocks = [];
+
+ expect(obj.shout()).to(equal, "HELLO");
+ });
+
+ });
+
describe("anonymous functions", function() {
before(function() {
foo = function() { return 'bar' };

0 comments on commit e759795

Please sign in to comment.