Skip to content

Commit

Permalink
Added a failing test that demonstrates side-effects of mocking global…
Browse files Browse the repository at this point in the history
… objects.
  • Loading branch information
Larry Karnowski committed Jul 17, 2009
1 parent be78e2d commit b95e8a7
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions spec/mock_spec.js
Expand Up @@ -73,7 +73,7 @@ Screw.Unit(function() {
expect(mockObj.foo('bar',baz)).to(equal, 'foobar');
});

it("should throw and arguments mismatched error if the arguments aren't matched", function() {
it("should throw an arguments mismatched error if the arguments aren't matched", function() {
mockObj = mock()
mockObj.should_receive('foo').with_arguments('bar').and_return('foobar');
try {
Expand All @@ -96,7 +96,7 @@ Screw.Unit(function() {
});
});

describe("added ontop of an existing object", function() {
describe("added on top of an existing object", function() {
before(function() {
obj = { say: "hello", shout: function() { return this.say.toUpperCase(); } }
mockObj = mock(obj);
Expand Down Expand Up @@ -124,7 +124,20 @@ Screw.Unit(function() {
it("should place expectations on existing methods destructively", function() {
myMock = mock({ say: "hello", shout: function() { throw "FAIL!" } });
myMock.should_receive('shout').exactly('once');
myMock.shout()
myMock.shout();
});
});

describe("idempotency 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");
expect(SomeGlobal.shout()).to(equal, "some string");
});

it("should not affect a later test", function(){
expect(SomeGlobal.shout()).to(equal, "HELLO");
});
});

Expand Down

0 comments on commit b95e8a7

Please sign in to comment.