You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_releases/latest/fakes.md
+29-7Lines changed: 29 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,36 +6,49 @@ breadcrumb: fakes
6
6
7
7
### Introduction
8
8
9
-
`fake`was introduced with Sinon with v5. It simplifies and merges concepts from [`spies`][spies] and [`stubs`][stubs].
9
+
`fake`is available in Sinon from v5 onwards. It allows creation of a `fake``Function` with the ability to set a default [behavior](#fakes-with-behavior). Set the [behavior](#fakes-with-behavior) using `Functions` with the same API as those in a [`sinon.stub`][stubs]. The created `fake``Function`, with or without behavior has the same API as a (`sinon.spy`)[spies].
10
10
11
11
In Sinon, a `fake` is a `Function` that records arguments, return value, the value of `this` and exception thrown (if any) for all of its calls.
12
12
13
-
It can be created with or without behavior; it can wrap an existing function.
14
-
15
13
A fake is immutable: once created, the behavior will not change.
16
14
17
15
Unlike [`sinon.spy`][spies] and [`sinon.stub`][stubs] methods, the `sinon.fake` API knows only how to create fakes, and doesn't concern itself with plugging them into the system under test. To plug the fakes into the system under test, you can use the [`sinon.replace*`](../sandbox#sandboxreplaceobject-property-replacement) methods.
18
16
19
-
20
17
### Creating a fake
21
18
19
+
Create a `fake``Function` with or without [behavior](#fakes-with-behavior). The created `Function` has the same API as a [`sinon.spy`][spies].
20
+
21
+
#### Creating a fake without behavior
22
+
22
23
```js
23
24
// create a basic fake, with no behavior
24
25
var fake =sinon.fake();
25
26
26
27
fake();
28
+
// undefined
27
29
28
-
console.log(fake.callCount);
30
+
fake.callCount;
29
31
// 1
30
32
```
31
33
34
+
#### Creating a fake with custom behaviour
35
+
36
+
```js
37
+
// create a fake that returns the text "foo"
38
+
var fake =sinon.fake.returns('foo');
39
+
40
+
fake()
41
+
// foo
42
+
```
43
+
32
44
### Fakes with behavior
33
45
34
-
Fakes can be created with behavior, which cannot be changed once the fake has been created.
46
+
Fakes cannot change once created with behaviour.
35
47
36
48
#### `sinon.fake.returns(value);`
37
49
38
-
Creates a fake that returns the `value` argument
50
+
Creates a fake that returns the `value` argument.
51
+
39
52
40
53
```js
41
54
var fake =sinon.fake.returns('apple pie');
@@ -48,6 +61,7 @@ fake();
48
61
49
62
Creates a fake that throws an `Error` with the provided value as the `message` property.
50
63
64
+
51
65
If an `Error` is passed as the `value` argument, then that will be the thrown value. If any other value is passed, then that will be used for the `message` property of the thrown `Error`.
52
66
53
67
```js
@@ -61,16 +75,19 @@ fake();
61
75
62
76
Creates a fake that returns a resolved `Promise` for the passed value.
63
77
78
+
64
79
#### `sinon.fake.rejects(value);`
65
80
66
81
Creates a fake that returns a rejected `Promise` for the passed value.
67
82
83
+
68
84
If an `Error` is passed as the `value` argument, then that will be the value of the promise. If any other value is passed, then that will be used for the `message` property of the `Error` returned by the promise.
69
85
70
86
#### `sinon.fake.yields([value1, ..., valueN]);`
71
87
72
88
`sinon.fake.yields` takes some values, and returns a function that when being called, expects the last argument to be a callback and invokes that callback with the same previously given values. The returned function is normally used to fake a service function that takes a callback as the last argument.
73
89
90
+
74
91
In code example below, the '[readFile](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback)' function of the 'fs' module is replaced with a fake function created by `sinon.fake.yields`. When the fake function is called, it always calls the last argument it received, which is expected to be a callback, with the values that the `yields` function previously took.
75
92
76
93
```js
@@ -85,6 +102,7 @@ console.log('end of this event loop');
85
102
86
103
Similar to `yields`, `yieldsAsync` also returns a function that when invoked, the function expects the last argument to be a callback and invokes that callback with the same previously given values. However, the returned function invokes that callback asynchronously rather than immediately, i.e. in the next event loop.
87
104
105
+
88
106
Compare the output of the code example below with the output of the code example above for `yields` to see the difference.
89
107
90
108
```js
@@ -100,10 +118,14 @@ console.log('end of this event loop');
100
118
101
119
Wraps an existing `Function` to record all interactions, while leaving it up to the `func` to provide the behavior.
102
120
121
+
The created `fake``Function` has the same API as a [`sinon.spy`][spies].
122
+
103
123
This is useful when complex behavior not covered by the `sinon.fake.*` methods is required or when wrapping an existing function or method.
104
124
105
125
### Instance properties
106
126
127
+
The instance properties are the same as a [`sinon.spy`][spies].
128
+
107
129
#### `f.callback`
108
130
109
131
This property is a convenience to get a reference to the last callback passed in the last to the fake.
0 commit comments