public
Description: A JavaScript mocking and stubbing framework
Clone URL: git://github.com/andykent/smoke.git
added a core file and improved stubs to auto sense if they are stubbing 
functions or methods.

- you can override the auto settings with
  - and_return_as_function()
  - and_return_as_property()
andykent (author)
Fri May 16 17:06:13 -0700 2008
commit  210e9da14a360658dccdeb8ba142c3154024771e
tree    a72294c369b16921786f4add2aa13fe95dbc500c
parent  6e247f814b450980c7dc4b38ba7ea489b555fc76
...
26
27
28
29
 
30
31
32
...
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
43
44
45
46
...
26
27
28
 
29
30
31
32
...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
0
@@ -26,7 +26,7 @@ Create your stubs...
0
 
0
   foo = {bar: function(attribute){return 'hello'}, baz:'goodbye'};
0
   new Smoke.Stub(foo,'baz').and_return('baz');
0
- new Smoke.Stub(foo,'bar()').and_return('???');
0
+ new Smoke.Stub(foo,'bar').and_return_as_function('blah');
0
 
0
 Create your mocks...
0
 
0
@@ -40,6 +40,19 @@ Check you expectations...
0
 
0
   Smoke.checkExpectations();
0
 
0
+More About Mocks
0
+----------------
0
+Mocks are the main part of the Smoke framework. But Smoke.Mock() has a bit of a dual personality depending on if you pass it a value.
0
+
0
+1. *without any arguments* it will return a fresh Mock with no more than default Object methods. You will need to mock all your interactions and check all your expectations.
0
+2. *with an argument* it will return a 'Mocked' version of the object that was passed in. This is very useful if you just want to mock a single method on your object whilst leaving the rest intact. It's especially helpful for just carrying out expectations on existing objects without any mocking at all.
0
+
0
+Smoke expectations are non-destructive meaning that if you add an expectation but don't specify a return value then the previous method will still be invoked (if one exists) and it's result will be returned.
0
+
0
+Known Issues
0
+------------
0
+* Whilst you can stub both functions and properties you can currently only carry out mock expectations on functions. Unfortunately I think this is a language limitation of JavaScript but if anyone has any bright ideas then I'm all ears.
0
+
0
 Contact
0
 -------
0
 Please send patches, comments or suggestions to andrew.d.kent@gmail.com
0
\ No newline at end of file
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
 
 
 
 
25
26
27
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
@@ -1,27 +1,23 @@
0
-Smoke = {
0
- mocks: [],
0
- passCount: 0,
0
- failCount: 0,
0
- print: function(v) {
0
- // use the jquery print plugin if ti is available or fall back to toString();
0
- return (jQuery && jQuery.print) ? $.print(v) : v.toString();
0
- },
0
- checkExpectations: function(){
0
- for(var i=0; i<Smoke.mocks.length; i++) Smoke.mocks[i].checkExpectations();
0
- },
0
- reset: function(){
0
- Smoke.mocks = [];
0
- passCount = 0;
0
- failCount = 0;
0
- },
0
- passed: function(mock){
0
- Smoke.passCount += 0;
0
- },
0
- failed: function(mock, message){
0
- Smoke.failCount += 1;
0
- throw(message);
0
- }
0
+Smoke.mocks = [];
0
+Smoke.passCount = 0;
0
+Smoke.failCount = 0;
0
+
0
+Smoke.checkExpectations = function(){
0
+ for(var i=0; i<Smoke.mocks.length; i++) Smoke.mocks[i].checkExpectations();
0
+};
0
+Smoke.reset = function(){
0
+ Smoke.mocks = [];
0
+ passCount = 0;
0
+ failCount = 0;
0
+};
0
+Smoke.passed = function(mock){
0
+ Smoke.passCount += 0;
0
 };
0
+Smoke.failed = function(mock, message){
0
+ Smoke.failCount += 1;
0
+ throw(message);
0
+};
0
+
0
 
0
 Smoke.Mock = function(originalObj) {
0
   var obj = originalObj || {} ;
...
18
19
20
21
22
 
 
23
24
25
 
26
 
27
28
29
 
 
30
31
 
32
33
34
...
18
19
20
 
 
21
22
23
24
 
25
26
27
28
 
 
29
30
31
32
33
34
35
36
0
@@ -18,16 +18,18 @@ Smoke.Stub.prototype = {
0
     return this
0
   },
0
   and_return: function(v){
0
- if(this.attribute.search(this.fnBracketMatch)>0) this.stubFunction(v);
0
- else this.stubProperty(v);
0
+ if(Smoke.isFunction(this.obj[this.attribute])) this.and_return_as_function(v);
0
+ else this.and_return_as_property(v);
0
     return this.obj
0
   },
0
- stubProperty: function(v){
0
+ and_return_as_property: function(v){
0
     this.obj[this.attribute] = v;
0
+ return this.obj
0
   },
0
- stubFunction: function(v){
0
- this.obj[this.attribute.replace(this.fnBracketMatch,'')] = function() {
0
+ and_return_as_function: function(v){
0
+ this.obj[this.attribute] = function() {
0
       return v
0
     };
0
+ return this.obj
0
   }
0
 };
0
\ No newline at end of file
...
2
3
4
5
6
 
 
7
8
9
...
2
3
4
 
 
5
6
7
8
9
0
@@ -2,8 +2,8 @@ Screw.Unit(function() {
0
   describe("mocking", function() {  
0
     describe("basics", function() {          
0
       it("allows stubbing directly on mock objects", function() {
0
- mockObj = mock().stub('bar()').and_return('bar');
0
- expect(mockObj.bar()).to(equal, 'bar');
0
+ mockObj = mock().stub('bar').and_return_as_function('baz');
0
+ expect(mockObj.bar()).to(equal, 'baz');
0
       });
0
     
0
       it("should check an exact call count", function() {
...
10
11
12
13
 
14
15
16
...
10
11
12
 
13
14
15
16
0
@@ -10,7 +10,7 @@ Screw.Unit(function() {
0
     });
0
     
0
     it("should return the stubbed value of a function", function() {
0
- stub(foo,'bar()').and_return('bar');
0
+ stub(foo,'bar').and_return('bar');
0
       expect(foo.bar()).to(equal, 'bar');
0
     });
0
   });
...
10
11
12
 
13
14
15
...
10
11
12
13
14
15
16
0
@@ -10,6 +10,7 @@
0
     <script src="su/screw.behaviors.js"></script>
0
     
0
     <!-- require smoke -->
0
+ <script src="../lib/smoke.core.js"></script>
0
     <script src="../lib/smoke.mock.js"></script>
0
     <script src="../lib/smoke.stub.js"></script>
0
 

Comments

    No one has commented yet.