New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fake docs improvement #2360
Merged
Merged
Fake docs improvement #2360
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af5c695
Clarify fake api, add missing references to spy api
srknzl 3687424
Mention return value in sandbox.replace()
srknzl 224c8e8
Merge remote-tracking branch 'upstream/master'
srknzl 8b2b89c
Move fake changes to latest docs
srknzl f6eb7f6
Move the sandbox change to latest docs
srknzl cbdea28
Merge remote-tracking branch 'upstream/master' into docs-fake-improve…
srknzl 232064e
Add fake runkit examples using existing spy examples
srknzl 61a4305
Fix pubsub fake example
srknzl 7b3895c
Change runnable examples to be the same with the ones in the current …
srknzl a8194a1
Add examples, address some review comments
srknzl File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,58 @@ breadcrumb: fakes | |
|
||
### Introduction | ||
|
||
`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]. | ||
`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]. | ||
|
||
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. | ||
|
||
A fake is immutable: once created, the behavior will not change. | ||
|
||
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. | ||
|
||
### When to use fakes? | ||
|
||
Fakes are alternatives to the Stubs and Spies, and they can fully replace all such use cases. | ||
|
||
They are intended to be simpler to use, and avoids many bugs by having immutable behaviour. | ||
|
||
The created `fake` `Function`, with or without behavior has the same API as a (`sinon.spy`)[spies]. | ||
|
||
#### Using fakes instead of spies | ||
|
||
```js | ||
var foo = { | ||
bar: () => { | ||
return 'baz'; | ||
} | ||
}; | ||
// wrap existing method without changing its behaviour | ||
var fake = sinon.replace(foo, 'bar', sinon.fake(foo.bar)); | ||
|
||
fake(); | ||
// baz | ||
|
||
fake.callCount; | ||
// 1 | ||
``` | ||
|
||
#### Using fakes instead of stubs | ||
|
||
```js | ||
var foo = { | ||
bar: () => { | ||
return 'baz'; | ||
} | ||
}; | ||
// replace method with a fake one | ||
var fake = sinon.replace(foo, 'bar', sinon.fake.returns('fake value')); | ||
|
||
fake(); | ||
// fake value | ||
|
||
fake.callCount; | ||
// 1 | ||
``` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this alternative use cases helps as I stated in the issue #2070 |
||
### Creating a fake | ||
|
||
Create a `fake` `Function` with or without [behavior](#fakes-with-behavior). The created `Function` has the same API as a [`sinon.spy`][spies]. | ||
|
@@ -123,7 +167,8 @@ This is useful when complex behavior not covered by the `sinon.fake.*` methods i | |
|
||
### Instance properties | ||
|
||
The instance properties are the same as a [`sinon.spy`][spies]. | ||
The instance properties are the same as a [`sinon.spy`][spies]. The following properties are just a few of them, you can refer to | ||
[spies][spies] documentation for all of them. | ||
|
||
#### `f.callback` | ||
|
||
|
@@ -141,7 +186,7 @@ f.callback === cb2; | |
// true | ||
``` | ||
|
||
The same convenience has been added to [spy calls](../spy-call): | ||
The same convenience has been added to [spy calls](../spy-call#spycallcallback): | ||
|
||
```js | ||
f.getCall(1).callback === cb2; | ||
|
@@ -167,6 +212,18 @@ f.firstArg === date2; | |
// true | ||
``` | ||
|
||
The same convenience has been added to [spy calls](../spy-call#spycallfirstarg): | ||
|
||
```js | ||
f.getCall(0).firstArg === date1; | ||
// true | ||
f.getCall(1).firstArg === date2; | ||
// true | ||
// | ||
f.lastCall.firstArg === date2; | ||
// true | ||
``` | ||
|
||
#### `f.lastArg` | ||
|
||
This property is a convenient way to get a reference to the last argument passed in the last call to the fake. | ||
|
@@ -183,7 +240,7 @@ f.lastArg === date2; | |
// true | ||
``` | ||
|
||
The same convenience has been added to [spy calls](../spy-call): | ||
The same convenience has been added to [spy calls](../spy-call#spycalllastarg): | ||
|
||
```js | ||
f.getCall(0).lastArg === date1; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought adding this new lines to introduction would be too much for an introduction section. So moved one line from there to here.