Skip to content

Commit 564172a

Browse files
committed
Document the new cool ways to create()
1 parent c732f72 commit 564172a

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

README.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,58 @@ At the moment, the library is only published to npm as a normal CommonJS module.
1616
If you'd like to use this for browser tests, please use Browserify or consider
1717
sending a pull request for [this issue](https://github.com/testdouble/testdouble.js/issues/10)
1818

19-
## Stub with `when()`
19+
## Create with `create()`
2020

21-
To stub with testdouble.js, first require it:
21+
The easiest way to create a test double function is to make one anonymously:
2222

2323
```
2424
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");
2537
```
2638

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
2843

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(){};
2953
```
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');
3071
myTestDouble = td.create();
3172
```
3273

0 commit comments

Comments
 (0)