Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 89 lines (60 sloc) 2.21 kb
93e3e231 »
2012-02-04 Initial working version.
1 # Sinon Mocha
2
b96672b5 »
2012-02-05 Updated README.md
3 Sinon Mocha is an extension library for mocha/sinon that will automatically clean up "wrapped" methods.
4
5 Tested with:
6
7 | **Sinon** | **Mocha** |
8 | ----- | ----- |
d23f87be »
2012-04-22 Bump tested version of mocha in readme
9 | 1.3 | ~1.0 |
b96672b5 »
2012-02-05 Updated README.md
10
11 As of version 0.0.1 browser support is not tested but is planned in a future version.
12
13 ## Example Usage
14
15 ``` javascript
16
17 var sinon = require('sinon'),
18 //You don't need to use expect for this to work
19 //This is just to provide a working example
20 expect = require('expect.js');
21
22 //Enhance sinon
23 //You can also pass a mocha as the second argument.
24 require('sinon-mocha').enhance(sinon);
25
26 describe("MyClass", function(){
27
28 var MyClass = require('my-class'),
29 subject;
30
31 //before (all) hooks are *NOT* automatically cleaned up for you.
32 before(function(){
33 sinon.stub(MyClass.prototype, 'ajax', function(){
34 //...
35 });
36 });
37
38 //You must `.restore` the method yourself.
39 after(function(){
40 MyClass.prototype.ajax.restore();
41 });
42
43 beforeEach(function(){
44 subject = new MyClass();
45
46 sinon.spy(subject, 'spiedMethod');
47 //Stubs also work
48 sinon.stub(subject, 'stubbedMethod');
49 //And stubs with functionality
50 sinon.stub(subject, 'stubbedMethodWFunc', function(){
51 console.log(' I get called ! ' );
52 });
53 });
54
55 describe(".spiedMethod", function(){
56
57 it("will call spy", function(){
58 subject.spiedMethod();
59 expect(subject.spiedMethod.called).to.be(true);
60 });
61
62 });
63
64 // Etc...
65
66 });
67
68 ```
69
70 ## How it works
71
72 In short sinon's wrapMethod function is overwritten to capture its results in the `beforeEach` of every test.
73 Then in the `afterEach` those methods are `.restore()`[d].
74
75 To gain access to mocha there is a hack that will search through `require.cache` and pick out its `mocha/index.js`
76 and require it to ensure we modify the actual mocha being used. I ran into some issue that lead me down this path.
77 When running `require('mocha')` I would end up with a different instance then was being used inside the test context.
78
79 `Mocha.Runner.runSuite` is patched to add a beforeEach and afterEach that invokes the sinon magic.
4021e0a0 »
2012-02-05 Added LICENSE and updated readme.md
80 See `lib/mocha-hooks.js`.
81
82 ## WHY?
83
84 Jasmine spoiled me.
85
86 ## License
87
88 MIT (see LICENSE)
Something went wrong with that request. Please try again.