Skip to content

Commit 45f1817

Browse files
committed
2 parents a783ab5 + f26915b commit 45f1817

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,41 @@ calculator.received().add(1, Arg.any());
2828
calculator.didNotReceive().add(2, 2);
2929
```
3030

31+
### Creating a mock
32+
`var myFakeCalculator = Substitute.for<Calculator>();`
33+
34+
### Setting return types
35+
See the example below.
36+
37+
```typescript
38+
//single return type
39+
calculator.add(1, 2).returns(4);
40+
console.log(calculator.add(1, 2)); //prints 4
41+
console.log(calculator.add(1, 2)); //prints undefined
42+
43+
//multiple return types in sequence
44+
calculator.add(1, 2).returns(3, 7, 9);
45+
console.log(calculator.add(1, 2)); //prints 3
46+
console.log(calculator.add(1, 2)); //prints 7
47+
console.log(calculator.add(1, 2)); //prints 9
48+
console.log(calculator.add(1, 2)); //prints undefined
49+
```
50+
51+
### Argument matchers
52+
There are several ways of matching arguments. You don't have to be explicit.
53+
54+
```typescript
55+
import { Arg } from '@fluffy-spoon/substitute';
56+
57+
//ignoring arguments
58+
calculator.add(Arg.any(), 2).returns(10);
59+
console.log(calculator.add(1337, 3)); //prints undefined since second argument doesn't match
60+
console.log(calculator.add(1337, 2)); //prints 10 since second argument matches
61+
calculator.received().add(Arg.any(), 3); //will not throw since a call matches
62+
63+
//ignoring arguments of specific type
64+
calculator.add(Arg.any('number'), 2).returns(10); //could also be 'array' or any string returned by the typeof operator
65+
```
66+
3167
## What is this - black magic?
3268
`@fluffy-spoon/substitute` works the same way that NSubstitute does, except that it uses the EcmaScript 6 `Proxy` class to produce the fakes. You can read more about how NSubstitute works to get inspired.

0 commit comments

Comments
 (0)