Skip to content

Commit f26915b

Browse files
authored
Update README.md
1 parent ba07c92 commit f26915b

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,37 @@ calculator.didNotReceive().add(2, 2);
3232
`var myFakeCalculator = Substitute.for<Calculator>();`
3333

3434
### Setting return types
35-
Multiple return types can be set in sequence. Consider the example below.
35+
See the example below.
3636

37-
```
38-
myFakeCalculator.add(1, 2).returns(3, 7, 9);
39-
console.log(myFakeCalculator.add(1, 2)); //prints 3
40-
console.log(myFakeCalculator.add(1, 2)); //prints 7
41-
console.log(myFakeCalculator.add(1, 2)); //prints 9
42-
console.log(myFakeCalculator.add(1, 2)); //prints undefined
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
4349
```
4450

45-
## Why TypeScript 3?
46-
Let's say I want to mock the type T, so I create a function `function mock<T>(): T`. If I then passed the object `{ doFooWithOneArgument: (input: string) => boolean, doFooWithTwoArguments: (input1: string, input2: boolean) => string }` to it, I would get a strong-typed variant of it in return.
51+
### Argument matchers
52+
There are several ways of matching arguments. You don't have to be explicit.
4753

48-
That's all good, but if I wanted to map that type into `{ doFooWithOneArgument: (whateverTheOneParameterTypeWas) => boolean, doWithTwoArguments: (whateverTheTwoParameterTypesWas) => string } & { returns: (...returnValuesInSequence: whateverTheReturnTypeOfDoFooWas[]) => void }`, I wouldn't be able to do it in a fully strong-typed manner, since there would be no way of expressing `whateverTheOneParameterTypeWas` and `whateverTheTwoParameterTypesWas` in a mapped type, and they would have to be defined as `...args: any[]`. However, now there's "Generic rest parameters" that can express this, as seen in [the example in the announcement post](https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#generic-rest-parameters).
54+
```typescript
55+
import { Arg } from '@fluffy-spoon/substitute';
4956

50-
See [this example TypeScript file](https://github.com/ffMathy/FluffySpoon.JavaScript.Testing/blob/master/src/Transformations.ts) for an example of how the type is defined. For each property of the original type given, I then handle it differently whether or not it's a function or a property by using the `infer` keyword. If it's a function, I then infer the arguments and its return type and re-express them in a strong-typed manner, but retaining the type safety of the arguments given. This was not possible before.
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
5162

52-
The end-result of this is that I can do something like `myFake.doWithTwoArguments('foo', false).returns("firstThis", "thenThis", "andThenThis")`, where both `'foo'` and `false` are strong-typed.
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+
```
5366

5467
## What is this - black magic?
5568
`@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)