public
Description: A JavaScript mocking and stubbing framework
Clone URL: git://github.com/andykent/smoke.git
can now mock onto real objects but expectations are currently destructive
andykent (author)
Fri May 16 15:17:06 -0700 2008
commit  0bfb830b9013d07c74ae992214a998f9cff98e60
tree    5c21106190c71a34d1ae514433c8cbc60acc0444
parent  b8e4d211e71671d03e5981142ae32ceeec3c5cdb
...
24
25
26
27
28
29
30
31
32
33
34
 
 
 
35
36
37
 
38
39
40
...
47
48
49
50
 
51
52
53
54
55
 
 
56
57
58
...
24
25
26
 
 
 
 
 
 
 
 
27
28
29
30
31
 
32
33
34
35
...
42
43
44
 
45
46
47
48
49
50
51
52
53
54
55
0
@@ -24,17 +24,12 @@ Smoke = {
0
 };
0
 
0
 Smoke.Mock = function(originalObj) {
0
- this._obj = originalObj || {} ;
0
- this._expectations = {};
0
- var mock = this; // closure var
0
- Smoke.mocks.push(mock)
0
-};
0
-
0
-Smoke.Mock.prototype = {
0
- stub: function(attr){
0
+ var obj = originalObj || {} ;
0
+ obj._expectations = {};
0
+ obj.stub = function(attr){
0
     return new Smoke.Stub(this, attr);
0
   },
0
- should_receive: function(attr){
0
+ obj.should_receive = function(attr){
0
     var expectation = new Smoke.Mock.Expectation(this, attr);
0
     if(this._expectations[attr]==undefined) this._expectations[attr] = [];
0
     this._expectations[attr].push(expectation);
0
@@ -47,12 +42,14 @@ Smoke.Mock.prototype = {
0
     };
0
     return expectation;
0
   },
0
- checkExpectations: function(){
0
+ obj.checkExpectations = function(){
0
     for(var e in this._expectations) {
0
       var expectations = this._expectations[e]
0
       for(var i in expectations) expectations[i].check();
0
     };
0
   },
0
+ Smoke.mocks.push(obj);
0
+ return obj;
0
 };
0
 
0
 Smoke.Mock.Expectation = function(mock, attr) {
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 // This is alightweight bridge between smoke and Screw.Unit
0
 // it shadows mocking and stubbing onto the matchers to make them available within tests
0
 Screw.Matchers.mock = function(m) {
0
- return new Smoke.Mock(m);
0
+ return Smoke.Mock(m);
0
 };
0
 
0
 Screw.Matchers.stub = function(obj,attr) {
...
61
62
63
64
 
65
66
67
68
69
 
70
71
72
73
 
74
75
76
 
 
 
 
77
78
 
 
 
 
 
79
80
81
82
 
 
 
83
84
85
86
...
61
62
63
 
64
65
66
67
68
 
69
70
71
72
 
73
74
75
 
76
77
78
79
80
 
81
82
83
84
85
86
87
88
 
89
90
91
92
93
94
95
0
@@ -61,25 +61,34 @@ Screw.Unit(function() {
0
     
0
     describe("an existing object", function() {
0
       before(function() {
0
- obj = { name: "Andy", shout: function() { return this.name.upperCase(); } }
0
+ obj = { say: "hello", shout: function() { return this.say.toUpperCase(); } }
0
         mockObj = mock(obj);
0
       });
0
       
0
       it("should leave original properties intact", function() {
0
- expect(mockObj.name).to(equal,'Andy');
0
+ expect(mockObj.say).to(equal,'hello');
0
       });
0
       
0
       it("should leave original functions intact", function() {
0
- expect(mockObj.shout).to(equal,'ANDY');
0
+ expect(mockObj.shout()).to(equal,'HELLO');
0
       });
0
       
0
- it("should add methods to allow stubbing and mocking on the objects properties");
0
+ it("should add methods to allow stubbing and mocking on the objects properties", function() {
0
+ expect(mockObj.should_receive).to_not(equal,undefined);
0
+ expect(mockObj.stub).to_not(equal,undefined);
0
+ });
0
       
0
- it("shouldn't break iterators")
0
+ it("shouldn't break Arrays", function() {
0
+ mockObj = mock([0,1,2,3]);
0
+ expect(mockObj[2]).to(equal,2);
0
+ expect(mockObj.length).to(equal,4);
0
+ });
0
     });
0
     
0
     describe("an objects prototype", function() {
0
- it("should allow mocks to be carried through to individual objects");
0
+ it("should allow mocks to be carried through to individual objects", function() {
0
+
0
+ });
0
     });
0
   });
0
 });
0
\ No newline at end of file

Comments

    No one has commented yet.