diff options
| author | mo k <mo@mokhan.ca> | 2012-05-21 12:06:48 -0600 |
|---|---|---|
| committer | mo k <mo@mokhan.ca> | 2012-05-21 12:06:48 -0600 |
| commit | ed7612d2dbf43c9250c3ed6a2f988ac29bd34c00 (patch) | |
| tree | f98b3d3e29a868e80e9758a8f99f2fae7d72e54e | |
| parent | 82fce20cd0f18829c3df13a7d62cacd75c09b9bf (diff) | |
move Fake to a separate file.
| -rw-r--r-- | public/javascripts/Fake.js | 65 | ||||
| -rw-r--r-- | spec/javascripts/FakeSpec.js | 68 |
2 files changed, 67 insertions, 66 deletions
diff --git a/public/javascripts/Fake.js b/public/javascripts/Fake.js new file mode 100644 index 0000000..bc59686 --- /dev/null +++ b/public/javascripts/Fake.js @@ -0,0 +1,65 @@ +var Fake = (function(){ + var fake = function(){ + _stubs = []; + this.stub = function(method){ + if(_stubs[method] === undefined) { + _stubs[method] = new Stub(this, method); + } + return _stubs[method]; + }; + }; + return function(){ + return new fake(); + }; +})(); + +var Stub = (function(){ + var stub = function(target, method){ + this.arguments = []; + this.with = function(){ + var args = new Arguments(this, target, method, arguments); + this.arguments.push(args); + return args; + }; + this.andReturn = function(return_value){ + target[method] = function(){ + return return_value; + }; + }; + this.find_match_for = function(args){ + for (var i = 0; i < this.arguments.length; i += 1) { + var matcher = this.arguments[i]; + if(matcher.matches(args)){ + return matcher; + } + } + throw "Matcher not found."; + }; + }; + return function(target, method){ + return new stub(target, method); + }; +})(); +var Arguments = (function(){ + var Arguments = function(stub, target, method, expected_args){ + this.andReturn = function(return_value){ + this.return_value = return_value; + + target[method] = function(){ + return stub.find_match_for(arguments).return_value; + }; + }; + this.matches = function(args){ + var result = expected_args === args; + for (var i = 0; i < expected_args.length; i += 1) { + if(args[i] !== expected_args[i]){ + return false; + } + } + return true; + }; + }; + return function(stub, target, method, expected_args){ + return new Arguments(stub, target, method, expected_args); + }; +})(); diff --git a/spec/javascripts/FakeSpec.js b/spec/javascripts/FakeSpec.js index 37ec967..3645c9b 100644 --- a/spec/javascripts/FakeSpec.js +++ b/spec/javascripts/FakeSpec.js @@ -3,6 +3,7 @@ describe ("Fake", function() { sut = new Fake(); }); var sut; + describe ("when stubbing out return values", function() { describe ("when there is no args", function() { it ("should return the correct value", function() { @@ -20,7 +21,7 @@ describe ("Fake", function() { expect(sut.greet('mo')).toEqual('hello mo'); }); it ("should return the correct value that corresponds to other args", function() { - //expect(sut.greet('jo')).toEqual('hello jo'); + expect(sut.greet('jo')).toEqual('hello jo'); }); beforeEach (function() { sut.stub('greet').with('mo').andReturn('hello mo'); @@ -30,68 +31,3 @@ describe ("Fake", function() { }); }); }); -var Fake = (function(){ - var fake = function(){ - _stubs = []; - this.stub = function(method){ - if(_stubs[method] === undefined) { - _stubs[method] = new Stub(this, method); - } - return _stubs[method]; - }; - }; - return function(){ - return new fake(); - }; -})(); - -var Stub = (function(){ - var stub = function(target, method){ - this.arguments = []; - this.with = function(){ - var args = new Arguments(this, target, method, arguments); - this.arguments.push(args); - return args; - }; - this.andReturn = function(return_value){ - target[method] = function(){ - return return_value; - }; - }; - this.find_match_for = function(args){ - for (var i = 0; i < this.arguments.length; i += 1) { - var matcher = this.arguments[i]; - if(matcher.matches(args)){ - return matcher; - } - } - throw "Matcher not found."; - }; - }; - return function(target, method){ - return new stub(target, method); - }; -})(); -var Arguments = (function(){ - var Arguments = function(stub, target, method, expected_args){ - this.andReturn = function(return_value){ - this.return_value = return_value; - - target[method] = function(){ - return stub.find_match_for(arguments).return_value; - }; - }; - this.matches = function(args){ - var result = expected_args === args; - for (var i = 0; i < expected_args.length; i += 1) { - if(args[i] !== expected_args[i]){ - return false; - } - } - return true; - }; - }; - return function(stub, target, method, expected_args){ - return new Arguments(stub, target, method, expected_args); - }; -})(); |
