@@ -16,17 +16,58 @@ At the moment, the library is only published to npm as a normal CommonJS module.
16
16
If you'd like to use this for browser tests, please use Browserify or consider
17
17
sending a pull request for [ this issue] ( https://github.com/testdouble/testdouble.js/issues/10 )
18
18
19
- ## Stub with ` when ()`
19
+ ## Create with ` create ()`
20
20
21
- To stub with testdouble.js, first require it :
21
+ The easiest way to create a test double function is to make one anonymously :
22
22
23
23
```
24
24
var td = require('testdouble');
25
+ var myTestDouble = td.create();
26
+ ```
27
+
28
+ In the above, ` myTestDouble ` will be able to be stubbed or verified as shown below.
29
+
30
+ ### Naming your test double
31
+
32
+ For slightly easier-to-understand error messages (with the trade-off of greater
33
+ redundancy in your tests), you can supply a string name to ` create `
34
+
35
+ ```
36
+ var myNamedDouble = td.create("#foo");
25
37
```
26
38
27
- Create a test double with the ` create ` function:
39
+ All error messages and descriptions provided for the above ` myNamedDouble ` will
40
+ also print the name ` #foo ` .
41
+
42
+ ### Creating test doubles for an entire type
28
43
44
+ It's very typical that the code under test will depend not only on a single
45
+ function, but on an object type that's full of them.
46
+
47
+ Suppose your subject has a dependency:
48
+
49
+ ``` javascript
50
+ function Dog () {};
51
+ Dog .prototype .bark = function (){};
52
+ Dog .prototype .bite = function (){};
29
53
```
54
+
55
+ Then you can create a test double of ` Dog ` with:
56
+
57
+ ``` javascript
58
+ var myDogDouble = td .create (Dog)
59
+ ```
60
+
61
+ This will return a plain object with ` bark ` and ` byte ` test double functions,
62
+ ready to be stubbed or verified and named ` "Dog#bark" ` and ` "Dog#bite" ` ,
63
+ respectively.
64
+
65
+ ## Stub with ` when() `
66
+
67
+ To stub values with testdouble.js, first create one:
68
+
69
+ ```
70
+ var td = require('testdouble');
30
71
myTestDouble = td.create();
31
72
```
32
73
0 commit comments