Permalink
Browse files

Fixed problems with expectations never being properly checked and add…

…ed tests to make sure that expections are thrown when expectations are not met.

Signed-off-by: Andy Kent <andrew.d.kent@gmail.com>
  • Loading branch information...
1 parent ef828a2 commit 3affbf6648f2cf71b44f70a2251cd5965c225fcc Irene Morente committed with andykent May 14, 2009
Showing with 28 additions and 2 deletions.
  1. +2 −2 lib/smoke.mock.js
  2. +26 −0 spec/mock_spec.js
View
4 lib/smoke.mock.js
@@ -43,7 +43,7 @@ Smoke.Mock = function(originalObj) {
obj.checkExpectations = function(){
for(var e in this._expectations) {
var expectations = this._expectations[e]
- for(var i; i < expectations.length; i++) expectations[i].check();
+ for(var i=0; i < expectations.length; i++) expectations[i].check();
};
},
Smoke.mocks.push(obj);
@@ -105,7 +105,7 @@ Smoke.Mock.Expectation.prototype = {
},
methodSignature: function(){
var a = '';
- var args = this.callerArgs;
+ var args = this.callerArgs || [];
for(var i=0; i<args.length; i++) a += Smoke.print(args[i])+', ';
a =a.slice(0,-2);
return this._attr+'('+a+')'
View
26 spec/mock_spec.js
@@ -13,6 +13,32 @@ Screw.Unit(function() {
m.bar();
});
+ it("should fail when an expectation is called too many times", function() {
+ var m = mock();
+ m.should_receive('bar').exactly('once');
+ m.bar();
+ m.bar();
+ try {
+ Smoke.checkExpectations();
+ throw("exception");
+ } catch(e) {
+ Smoke.reset();
+ expect(e).to(equal, 'expected bar() to be called exactly 1 times but it got called 2 times');
+ }
+ });
+
+ it("should fail when an expectation is set and not called", function() {
+ var m = mock();
+ m.should_receive('bar').exactly('once');
+ try {
+ Smoke.checkExpectations();
+ throw("exception");
+ } catch(e) {
+ Smoke.reset();
+ expect(e).to(equal, 'expected bar() to be called exactly 1 times but it got called 0 times');
+ }
+ });
+
it("should not check arguments when with_arguments is not used", function() {
var m = mock()
m.should_receive('bar').exactly('once');

0 comments on commit 3affbf6

Please sign in to comment.