From af5c695b025c8563a18685adec1bf7a89b450d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96zel?= Date: Fri, 23 Apr 2021 16:45:11 +0300 Subject: [PATCH 1/8] Clarify fake api, add missing references to spy api --- docs/_releases/v10.0.1/fakes.md | 65 +++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/docs/_releases/v10.0.1/fakes.md b/docs/_releases/v10.0.1/fakes.md index 424db61e6..5c46d4fcb 100644 --- a/docs/_releases/v10.0.1/fakes.md +++ b/docs/_releases/v10.0.1/fakes.md @@ -6,7 +6,7 @@ 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. @@ -14,6 +14,50 @@ 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 +``` + ### 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; From 368742407c12aa1f135087365ff0a024ca573b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96zel?= Date: Fri, 23 Apr 2021 16:45:58 +0300 Subject: [PATCH 2/8] Mention return value in sandbox.replace() --- docs/_releases/v10.0.1/sandbox.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_releases/v10.0.1/sandbox.md b/docs/_releases/v10.0.1/sandbox.md index 74803acf7..b4c770443 100644 --- a/docs/_releases/v10.0.1/sandbox.md +++ b/docs/_releases/v10.0.1/sandbox.md @@ -183,7 +183,7 @@ _Since `sinon@2.0.0`_ #### `sandbox.replace(object, property, replacement);` -Replaces `property` on `object` with `replacement` argument. Attempts to replace an already replaced value cause an exception. +Replaces `property` on `object` with `replacement` argument. Attempts to replace an already replaced value cause an exception. Returns the `replacement`. `replacement` can be any value, including `spies`, `stubs` and `fakes`. From 8b2b89cc1c56f0f2d5146f9df85d5ec29f27dbad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96zel?= Date: Mon, 26 Apr 2021 22:04:34 +0300 Subject: [PATCH 3/8] Move fake changes to latest docs --- docs/_releases/v10.0.1/fakes.md | 65 ++-------------------------- docs/release-source/release/fakes.md | 65 ++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/docs/_releases/v10.0.1/fakes.md b/docs/_releases/v10.0.1/fakes.md index 5c46d4fcb..424db61e6 100644 --- a/docs/_releases/v10.0.1/fakes.md +++ b/docs/_releases/v10.0.1/fakes.md @@ -6,7 +6,7 @@ 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]. +`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]. 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. @@ -14,50 +14,6 @@ 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 -``` - ### 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]. @@ -167,8 +123,7 @@ 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 following properties are just a few of them, you can refer to -[spies][spies] documentation for all of them. +The instance properties are the same as a [`sinon.spy`][spies]. #### `f.callback` @@ -186,7 +141,7 @@ f.callback === cb2; // true ``` -The same convenience has been added to [spy calls](../spy-call#spycallcallback): +The same convenience has been added to [spy calls](../spy-call): ```js f.getCall(1).callback === cb2; @@ -212,18 +167,6 @@ 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. @@ -240,7 +183,7 @@ f.lastArg === date2; // true ``` -The same convenience has been added to [spy calls](../spy-call#spycalllastarg): +The same convenience has been added to [spy calls](../spy-call): ```js f.getCall(0).lastArg === date1; diff --git a/docs/release-source/release/fakes.md b/docs/release-source/release/fakes.md index 424db61e6..db604ec85 100644 --- a/docs/release-source/release/fakes.md +++ b/docs/release-source/release/fakes.md @@ -6,7 +6,7 @@ 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. @@ -14,6 +14,50 @@ 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 +``` + ### 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; From f6eb7f657276ddbbc55b81ae44ff6934c533db74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96zel?= Date: Mon, 26 Apr 2021 22:09:18 +0300 Subject: [PATCH 4/8] Move the sandbox change to latest docs --- docs/_releases/v10.0.1/sandbox.md | 2 +- docs/release-source/release/sandbox.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/_releases/v10.0.1/sandbox.md b/docs/_releases/v10.0.1/sandbox.md index b4c770443..74803acf7 100644 --- a/docs/_releases/v10.0.1/sandbox.md +++ b/docs/_releases/v10.0.1/sandbox.md @@ -183,7 +183,7 @@ _Since `sinon@2.0.0`_ #### `sandbox.replace(object, property, replacement);` -Replaces `property` on `object` with `replacement` argument. Attempts to replace an already replaced value cause an exception. Returns the `replacement`. +Replaces `property` on `object` with `replacement` argument. Attempts to replace an already replaced value cause an exception. `replacement` can be any value, including `spies`, `stubs` and `fakes`. diff --git a/docs/release-source/release/sandbox.md b/docs/release-source/release/sandbox.md index 8f346ff09..822c9ce4d 100644 --- a/docs/release-source/release/sandbox.md +++ b/docs/release-source/release/sandbox.md @@ -183,7 +183,7 @@ _Since `sinon@2.0.0`_ #### `sandbox.replace(object, property, replacement);` -Replaces `property` on `object` with `replacement` argument. Attempts to replace an already replaced value cause an exception. +Replaces `property` on `object` with `replacement` argument. Attempts to replace an already replaced value cause an exception. Returns the `replacement`. `replacement` can be any value, including `spies`, `stubs` and `fakes`. From 232064e5659c0f4f758e29530bbbc0cac34137ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96zel?= Date: Sun, 2 May 2021 11:28:48 +0300 Subject: [PATCH 5/8] Add fake runkit examples using existing spy examples --- .../release/examples/fakes-1-pubsub.test.js | 16 +++++++++ .../fakes-2-wrap-existing-method.test.js | 33 +++++++++++++++++++ .../examples/fakes-3-pubsub-message-1.test.js | 18 ++++++++++ .../examples/fakes-4-pubsub-message-2.test.js | 18 ++++++++++ .../examples/fakes-5-pubsub-message-3.test.js | 18 ++++++++++ .../examples/fakes-6-with-args.test.js | 18 ++++++++++ .../release/examples/fakes-7-spy-call.test.js | 33 +++++++++++++++++++ 7 files changed, 154 insertions(+) create mode 100644 docs/release-source/release/examples/fakes-1-pubsub.test.js create mode 100644 docs/release-source/release/examples/fakes-2-wrap-existing-method.test.js create mode 100644 docs/release-source/release/examples/fakes-3-pubsub-message-1.test.js create mode 100644 docs/release-source/release/examples/fakes-4-pubsub-message-2.test.js create mode 100644 docs/release-source/release/examples/fakes-5-pubsub-message-3.test.js create mode 100644 docs/release-source/release/examples/fakes-6-with-args.test.js create mode 100644 docs/release-source/release/examples/fakes-7-spy-call.test.js diff --git a/docs/release-source/release/examples/fakes-1-pubsub.test.js b/docs/release-source/release/examples/fakes-1-pubsub.test.js new file mode 100644 index 000000000..f147b922f --- /dev/null +++ b/docs/release-source/release/examples/fakes-1-pubsub.test.js @@ -0,0 +1,16 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const PubSub = require("pubsub-js"); +const referee = require("@sinonjs/referee"); +const assertTrue = referee.assert; + +describe("PubSub", function () { + it("should call subscribers on publish", function () { + const callback = sinon.spy(); + + PubSub.subscribe("message", callback); + PubSub.publishSync("message"); + + assertTrue(callback.called); + }); +}); diff --git a/docs/release-source/release/examples/fakes-2-wrap-existing-method.test.js b/docs/release-source/release/examples/fakes-2-wrap-existing-method.test.js new file mode 100644 index 000000000..0c4a1de48 --- /dev/null +++ b/docs/release-source/release/examples/fakes-2-wrap-existing-method.test.js @@ -0,0 +1,33 @@ +require("@fatso83/mini-mocha").install(); + +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; +const jsdom = require("jsdom"); +const JSDOM = jsdom.JSDOM; +const window = new JSDOM().window; +const document = new JSDOM("").window; +const jQuery = require("jquery")(window); +global.document = document; + +describe("Wrap existing method", function () { + const sandbox = sinon.createSandbox(); + let fakeAjax; + + beforeEach(function () { + fakeAjax = sandbox.replace(jQuery, "ajax", sandbox.fake()); + }); + + afterEach(function () { + sandbox.restore(); + }); + + it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () { + const url = "https://jsonplaceholder.typicode.com/todos/1"; + jQuery.getJSON(url); + + assert(fakeAjax.calledOnce); + assert.equals(url, fakeAjax.getCall(0).args[0].url); + assert.equals("json", fakeAjax.getCall(0).args[0].dataType); + }); +}); diff --git a/docs/release-source/release/examples/fakes-3-pubsub-message-1.test.js b/docs/release-source/release/examples/fakes-3-pubsub-message-1.test.js new file mode 100644 index 000000000..c5762c1ef --- /dev/null +++ b/docs/release-source/release/examples/fakes-3-pubsub-message-1.test.js @@ -0,0 +1,18 @@ +require("@fatso83/mini-mocha").install(); + +const sinon = require("sinon"); +const PubSub = require("pubsub-js"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("PubSub", function () { + it("should call subscribers with message as first argument", function () { + const message = "an example message"; + const fake = sinon.fake(); + + PubSub.subscribe(message, fake); + PubSub.publishSync(message, "some payload"); + + assert(fake.calledWith(message)); + }); +}); diff --git a/docs/release-source/release/examples/fakes-4-pubsub-message-2.test.js b/docs/release-source/release/examples/fakes-4-pubsub-message-2.test.js new file mode 100644 index 000000000..f78aa9a39 --- /dev/null +++ b/docs/release-source/release/examples/fakes-4-pubsub-message-2.test.js @@ -0,0 +1,18 @@ +require("@fatso83/mini-mocha").install(); + +const sinon = require("sinon"); +const PubSub = require("pubsub-js"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("PubSub", function () { + it("should call subscribers with message as first argument", function () { + const message = "an example message"; + const fake = sinon.fake(); + + PubSub.subscribe(message, fake); + PubSub.publishSync(message, "some payload"); + + assert.equals(message, fake.args[0][0]); + }); +}); diff --git a/docs/release-source/release/examples/fakes-5-pubsub-message-3.test.js b/docs/release-source/release/examples/fakes-5-pubsub-message-3.test.js new file mode 100644 index 000000000..43db5c4bf --- /dev/null +++ b/docs/release-source/release/examples/fakes-5-pubsub-message-3.test.js @@ -0,0 +1,18 @@ +require("@fatso83/mini-mocha").install(); + +const sinon = require("sinon"); +const PubSub = require("pubsub-js"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("PubSub", function () { + it("should call subscribers with message as first argument", function () { + const message = "an example message"; + const fake = sinon.fake(); + + PubSub.subscribe(message, fake); + PubSub.publishSync(message, "some payload"); + + assert.equals(message, fake.getCall(0).args[0]); + }); +}); diff --git a/docs/release-source/release/examples/fakes-6-with-args.test.js b/docs/release-source/release/examples/fakes-6-with-args.test.js new file mode 100644 index 000000000..a5fdbebd0 --- /dev/null +++ b/docs/release-source/release/examples/fakes-6-with-args.test.js @@ -0,0 +1,18 @@ +require("@fatso83/mini-mocha").install(); + +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("withArgs", function () { + it("should call method once with each argument", function () { + const object = { method() {} }; + const fake = sinon.replace(object, "method", sinon.fake()); + + object.method(42); + object.method(1); + + assert(fake.calledWith(42)); + assert(fake.calledWith(1)); + }); +}); diff --git a/docs/release-source/release/examples/fakes-7-spy-call.test.js b/docs/release-source/release/examples/fakes-7-spy-call.test.js new file mode 100644 index 000000000..6e59cfff0 --- /dev/null +++ b/docs/release-source/release/examples/fakes-7-spy-call.test.js @@ -0,0 +1,33 @@ +require("@fatso83/mini-mocha").install(); + +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; +const jsdom = require("jsdom"); +const JSDOM = jsdom.JSDOM; +const window = new JSDOM().window; +const document = new JSDOM("").window; +const jQuery = require("jquery")(window); +global.document = document; + +describe("Return nth call", function () { + const sandbox = sinon.createSandbox(); + + let fakeAjax; + + beforeEach(function () { + fakeAjax = sandbox.replace(jQuery, "ajax", sandbox.fake()); + }); + + afterEach(function () { + sandbox.restore(); + }); + + it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () { + const url = "https://jsonplaceholder.typicode.com/todos/1"; + fakeAjax(url); + const fakeCall = fakeAjax.getCall(0); + + assert.equals(url, fakeCall.args[0]); + }); +}); From 61a43059c797749bb9a05dcfe511564ae040bd0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96zel?= Date: Sun, 2 May 2021 11:35:26 +0300 Subject: [PATCH 6/8] Fix pubsub fake example --- docs/release-source/release/examples/fakes-1-pubsub.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/release-source/release/examples/fakes-1-pubsub.test.js b/docs/release-source/release/examples/fakes-1-pubsub.test.js index f147b922f..009da8fe6 100644 --- a/docs/release-source/release/examples/fakes-1-pubsub.test.js +++ b/docs/release-source/release/examples/fakes-1-pubsub.test.js @@ -6,7 +6,7 @@ const assertTrue = referee.assert; describe("PubSub", function () { it("should call subscribers on publish", function () { - const callback = sinon.spy(); + const callback = sinon.fake(); PubSub.subscribe("message", callback); PubSub.publishSync("message"); From 7b3895cba07e520217dda9ab2880d6abe017330f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96zel?= Date: Sat, 8 May 2021 18:03:10 +0300 Subject: [PATCH 7/8] Change runnable examples to be the same with the ones in the current documentation --- ...fakes-1-creating-without-behaviour.test.js | 15 +++++++++ .../release/examples/fakes-1-pubsub.test.js | 16 --------- ...0-adding-fake-to-system-under-test.test.js | 20 +++++++++++ ...s-2-creating-with-custom-behaviour.test.js | 13 ++++++++ .../fakes-2-wrap-existing-method.test.js | 33 ------------------- .../examples/fakes-3-pubsub-message-1.test.js | 18 ---------- .../release/examples/fakes-3-returns.test.js | 12 +++++++ .../examples/fakes-4-pubsub-message-2.test.js | 18 ---------- .../release/examples/fakes-4-throws.test.js | 13 ++++++++ .../examples/fakes-5-pubsub-message-3.test.js | 18 ---------- .../release/examples/fakes-5-yields.test.js | 21 ++++++++++++ .../examples/fakes-6-with-args.test.js | 18 ---------- .../examples/fakes-6-yields-async.test.js | 21 ++++++++++++ .../release/examples/fakes-7-callback.test.js | 19 +++++++++++ .../release/examples/fakes-7-spy-call.test.js | 33 ------------------- .../release/examples/fakes-8-firstArg.test.js | 17 ++++++++++ .../release/examples/fakes-9-lastArg.test.js | 20 +++++++++++ 17 files changed, 171 insertions(+), 154 deletions(-) create mode 100644 docs/release-source/release/examples/fakes-1-creating-without-behaviour.test.js delete mode 100644 docs/release-source/release/examples/fakes-1-pubsub.test.js create mode 100644 docs/release-source/release/examples/fakes-10-adding-fake-to-system-under-test.test.js create mode 100644 docs/release-source/release/examples/fakes-2-creating-with-custom-behaviour.test.js delete mode 100644 docs/release-source/release/examples/fakes-2-wrap-existing-method.test.js delete mode 100644 docs/release-source/release/examples/fakes-3-pubsub-message-1.test.js create mode 100644 docs/release-source/release/examples/fakes-3-returns.test.js delete mode 100644 docs/release-source/release/examples/fakes-4-pubsub-message-2.test.js create mode 100644 docs/release-source/release/examples/fakes-4-throws.test.js delete mode 100644 docs/release-source/release/examples/fakes-5-pubsub-message-3.test.js create mode 100644 docs/release-source/release/examples/fakes-5-yields.test.js delete mode 100644 docs/release-source/release/examples/fakes-6-with-args.test.js create mode 100644 docs/release-source/release/examples/fakes-6-yields-async.test.js create mode 100644 docs/release-source/release/examples/fakes-7-callback.test.js delete mode 100644 docs/release-source/release/examples/fakes-7-spy-call.test.js create mode 100644 docs/release-source/release/examples/fakes-8-firstArg.test.js create mode 100644 docs/release-source/release/examples/fakes-9-lastArg.test.js diff --git a/docs/release-source/release/examples/fakes-1-creating-without-behaviour.test.js b/docs/release-source/release/examples/fakes-1-creating-without-behaviour.test.js new file mode 100644 index 000000000..1164a1d30 --- /dev/null +++ b/docs/release-source/release/examples/fakes-1-creating-without-behaviour.test.js @@ -0,0 +1,15 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should create fake without behaviour", function () { + // create a basic fake, with no behavior + const fake = sinon.fake(); + + assert.isUndefined(fake()); + + assert.equals(fake.callCount, 1); + }); +}); diff --git a/docs/release-source/release/examples/fakes-1-pubsub.test.js b/docs/release-source/release/examples/fakes-1-pubsub.test.js deleted file mode 100644 index 009da8fe6..000000000 --- a/docs/release-source/release/examples/fakes-1-pubsub.test.js +++ /dev/null @@ -1,16 +0,0 @@ -require("@fatso83/mini-mocha").install(); -const sinon = require("sinon"); -const PubSub = require("pubsub-js"); -const referee = require("@sinonjs/referee"); -const assertTrue = referee.assert; - -describe("PubSub", function () { - it("should call subscribers on publish", function () { - const callback = sinon.fake(); - - PubSub.subscribe("message", callback); - PubSub.publishSync("message"); - - assertTrue(callback.called); - }); -}); diff --git a/docs/release-source/release/examples/fakes-10-adding-fake-to-system-under-test.test.js b/docs/release-source/release/examples/fakes-10-adding-fake-to-system-under-test.test.js new file mode 100644 index 000000000..d8c121558 --- /dev/null +++ b/docs/release-source/release/examples/fakes-10-adding-fake-to-system-under-test.test.js @@ -0,0 +1,20 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should have working lastArg property", function () { + const f = sinon.fake(); + const date1 = new Date(); + const date2 = new Date(); + + f(1, 2, date1); + f(1, 2, date2); + + assert.isTrue(f.lastArg === date2); + assert.isTrue(f.getCall(0).lastArg === date1); + assert.isTrue(f.getCall(1).lastArg === date2); + assert.isTrue(f.lastCall.lastArg === date2); + }); +}); diff --git a/docs/release-source/release/examples/fakes-2-creating-with-custom-behaviour.test.js b/docs/release-source/release/examples/fakes-2-creating-with-custom-behaviour.test.js new file mode 100644 index 000000000..ebb769a8a --- /dev/null +++ b/docs/release-source/release/examples/fakes-2-creating-with-custom-behaviour.test.js @@ -0,0 +1,13 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should create fake with custom behaviour", function () { + // create a fake that returns the text "foo" + const fake = sinon.fake.returns("foo"); + + assert.equals(fake(), "foo"); + }); +}); diff --git a/docs/release-source/release/examples/fakes-2-wrap-existing-method.test.js b/docs/release-source/release/examples/fakes-2-wrap-existing-method.test.js deleted file mode 100644 index 0c4a1de48..000000000 --- a/docs/release-source/release/examples/fakes-2-wrap-existing-method.test.js +++ /dev/null @@ -1,33 +0,0 @@ -require("@fatso83/mini-mocha").install(); - -const sinon = require("sinon"); -const referee = require("@sinonjs/referee"); -const assert = referee.assert; -const jsdom = require("jsdom"); -const JSDOM = jsdom.JSDOM; -const window = new JSDOM().window; -const document = new JSDOM("").window; -const jQuery = require("jquery")(window); -global.document = document; - -describe("Wrap existing method", function () { - const sandbox = sinon.createSandbox(); - let fakeAjax; - - beforeEach(function () { - fakeAjax = sandbox.replace(jQuery, "ajax", sandbox.fake()); - }); - - afterEach(function () { - sandbox.restore(); - }); - - it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () { - const url = "https://jsonplaceholder.typicode.com/todos/1"; - jQuery.getJSON(url); - - assert(fakeAjax.calledOnce); - assert.equals(url, fakeAjax.getCall(0).args[0].url); - assert.equals("json", fakeAjax.getCall(0).args[0].dataType); - }); -}); diff --git a/docs/release-source/release/examples/fakes-3-pubsub-message-1.test.js b/docs/release-source/release/examples/fakes-3-pubsub-message-1.test.js deleted file mode 100644 index c5762c1ef..000000000 --- a/docs/release-source/release/examples/fakes-3-pubsub-message-1.test.js +++ /dev/null @@ -1,18 +0,0 @@ -require("@fatso83/mini-mocha").install(); - -const sinon = require("sinon"); -const PubSub = require("pubsub-js"); -const referee = require("@sinonjs/referee"); -const assert = referee.assert; - -describe("PubSub", function () { - it("should call subscribers with message as first argument", function () { - const message = "an example message"; - const fake = sinon.fake(); - - PubSub.subscribe(message, fake); - PubSub.publishSync(message, "some payload"); - - assert(fake.calledWith(message)); - }); -}); diff --git a/docs/release-source/release/examples/fakes-3-returns.test.js b/docs/release-source/release/examples/fakes-3-returns.test.js new file mode 100644 index 000000000..092475d93 --- /dev/null +++ b/docs/release-source/release/examples/fakes-3-returns.test.js @@ -0,0 +1,12 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should create a fake that 'returns' a value", function () { + const fake = sinon.fake.returns("apple pie"); + + assert.equals(fake(), "apple pie"); + }); +}); diff --git a/docs/release-source/release/examples/fakes-4-pubsub-message-2.test.js b/docs/release-source/release/examples/fakes-4-pubsub-message-2.test.js deleted file mode 100644 index f78aa9a39..000000000 --- a/docs/release-source/release/examples/fakes-4-pubsub-message-2.test.js +++ /dev/null @@ -1,18 +0,0 @@ -require("@fatso83/mini-mocha").install(); - -const sinon = require("sinon"); -const PubSub = require("pubsub-js"); -const referee = require("@sinonjs/referee"); -const assert = referee.assert; - -describe("PubSub", function () { - it("should call subscribers with message as first argument", function () { - const message = "an example message"; - const fake = sinon.fake(); - - PubSub.subscribe(message, fake); - PubSub.publishSync(message, "some payload"); - - assert.equals(message, fake.args[0][0]); - }); -}); diff --git a/docs/release-source/release/examples/fakes-4-throws.test.js b/docs/release-source/release/examples/fakes-4-throws.test.js new file mode 100644 index 000000000..ec42e9576 --- /dev/null +++ b/docs/release-source/release/examples/fakes-4-throws.test.js @@ -0,0 +1,13 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should create a fake that 'throws' an Error", function () { + const fake = sinon.fake.throws(new Error("not apple pie")); + + // Expect to throw an error with message 'not apple pie' + assert.exception(fake, { name: "Error", message: "not apple pie" }); + }); +}); diff --git a/docs/release-source/release/examples/fakes-5-pubsub-message-3.test.js b/docs/release-source/release/examples/fakes-5-pubsub-message-3.test.js deleted file mode 100644 index 43db5c4bf..000000000 --- a/docs/release-source/release/examples/fakes-5-pubsub-message-3.test.js +++ /dev/null @@ -1,18 +0,0 @@ -require("@fatso83/mini-mocha").install(); - -const sinon = require("sinon"); -const PubSub = require("pubsub-js"); -const referee = require("@sinonjs/referee"); -const assert = referee.assert; - -describe("PubSub", function () { - it("should call subscribers with message as first argument", function () { - const message = "an example message"; - const fake = sinon.fake(); - - PubSub.subscribe(message, fake); - PubSub.publishSync(message, "some payload"); - - assert.equals(message, fake.getCall(0).args[0]); - }); -}); diff --git a/docs/release-source/release/examples/fakes-5-yields.test.js b/docs/release-source/release/examples/fakes-5-yields.test.js new file mode 100644 index 000000000..639d1c277 --- /dev/null +++ b/docs/release-source/release/examples/fakes-5-yields.test.js @@ -0,0 +1,21 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; +const fs = require("fs"); + +describe("FakeTest", function () { + it("should create a fake that 'yields'", function () { + const fake = sinon.fake.yields(null, "file content"); + const anotherFake = sinon.fake(); + + sinon.replace(fs, "readFile", fake); + fs.readFile("somefile", (err, data) => { + assert.isNull(err); + assert.equals(data, "file content"); + assert.isFalse(anotherFake.called); + }); + + anotherFake(); + }); +}); diff --git a/docs/release-source/release/examples/fakes-6-with-args.test.js b/docs/release-source/release/examples/fakes-6-with-args.test.js deleted file mode 100644 index a5fdbebd0..000000000 --- a/docs/release-source/release/examples/fakes-6-with-args.test.js +++ /dev/null @@ -1,18 +0,0 @@ -require("@fatso83/mini-mocha").install(); - -const sinon = require("sinon"); -const referee = require("@sinonjs/referee"); -const assert = referee.assert; - -describe("withArgs", function () { - it("should call method once with each argument", function () { - const object = { method() {} }; - const fake = sinon.replace(object, "method", sinon.fake()); - - object.method(42); - object.method(1); - - assert(fake.calledWith(42)); - assert(fake.calledWith(1)); - }); -}); diff --git a/docs/release-source/release/examples/fakes-6-yields-async.test.js b/docs/release-source/release/examples/fakes-6-yields-async.test.js new file mode 100644 index 000000000..6012c416c --- /dev/null +++ b/docs/release-source/release/examples/fakes-6-yields-async.test.js @@ -0,0 +1,21 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; +const fs = require("fs"); + +describe("FakeTest", function () { + it("should create a fake that 'yields asynchronously'", function () { + const fake = sinon.fake.yieldsAsync(null, "file content"); + const anotherFake = sinon.fake(); + + sinon.replace(fs, "readFile", fake); + fs.readFile("somefile", (err, data) => { + assert.isNull(err); + assert.equals(data, "file content"); + assert.isTrue(anotherFake.called); + }); + + anotherFake(); + }); +}); diff --git a/docs/release-source/release/examples/fakes-7-callback.test.js b/docs/release-source/release/examples/fakes-7-callback.test.js new file mode 100644 index 000000000..d22bdef56 --- /dev/null +++ b/docs/release-source/release/examples/fakes-7-callback.test.js @@ -0,0 +1,19 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should have working callback property", function () { + const f = sinon.fake(); + const cb1 = function () {}; + const cb2 = function () {}; + + f(1, 2, 3, cb1); + f(1, 2, 3, cb2); + + assert.isTrue(f.callback === cb2); + assert.isTrue(f.getCall(1).callback === cb2); + assert.isTrue(f.lastCall.callback === cb2); + }); +}); diff --git a/docs/release-source/release/examples/fakes-7-spy-call.test.js b/docs/release-source/release/examples/fakes-7-spy-call.test.js deleted file mode 100644 index 6e59cfff0..000000000 --- a/docs/release-source/release/examples/fakes-7-spy-call.test.js +++ /dev/null @@ -1,33 +0,0 @@ -require("@fatso83/mini-mocha").install(); - -const sinon = require("sinon"); -const referee = require("@sinonjs/referee"); -const assert = referee.assert; -const jsdom = require("jsdom"); -const JSDOM = jsdom.JSDOM; -const window = new JSDOM().window; -const document = new JSDOM("").window; -const jQuery = require("jquery")(window); -global.document = document; - -describe("Return nth call", function () { - const sandbox = sinon.createSandbox(); - - let fakeAjax; - - beforeEach(function () { - fakeAjax = sandbox.replace(jQuery, "ajax", sandbox.fake()); - }); - - afterEach(function () { - sandbox.restore(); - }); - - it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () { - const url = "https://jsonplaceholder.typicode.com/todos/1"; - fakeAjax(url); - const fakeCall = fakeAjax.getCall(0); - - assert.equals(url, fakeCall.args[0]); - }); -}); diff --git a/docs/release-source/release/examples/fakes-8-firstArg.test.js b/docs/release-source/release/examples/fakes-8-firstArg.test.js new file mode 100644 index 000000000..5b047bf0b --- /dev/null +++ b/docs/release-source/release/examples/fakes-8-firstArg.test.js @@ -0,0 +1,17 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should have working firstArg property", function () { + const f = sinon.fake(); + const date1 = new Date(); + const date2 = new Date(); + + f(date1, 1, 2); + f(date2, 1, 2); + + assert.isTrue(f.firstArg === date2); + }); +}); diff --git a/docs/release-source/release/examples/fakes-9-lastArg.test.js b/docs/release-source/release/examples/fakes-9-lastArg.test.js new file mode 100644 index 000000000..9a5e18304 --- /dev/null +++ b/docs/release-source/release/examples/fakes-9-lastArg.test.js @@ -0,0 +1,20 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should have working lastArg property", function () { + const fake = sinon.fake.returns("42"); + + sinon.replace(console, "log", fake); + + assert.equals(console.log("apple pie"), 42); + + // restores all replaced properties set by sinon methods (replace, spy, stub) + sinon.restore(); + + assert.equals(console.log("apple pie"), undefined); + assert.equals(fake.callCount, 1); + }); +}); From a8194a14fc602a63ae2f004b32c12ff31733cd72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96zel?= Date: Sat, 8 May 2021 22:04:40 +0300 Subject: [PATCH 8/8] Add examples, address some review comments --- ...kes-1-using-fakes-instead-of-spies.test.js | 17 +++++++++++++++ ...tArg.test.js => fakes-10-firstArg.test.js} | 0 ...-test.test.js => fakes-11-lastArg.test.js} | 1 + ...-adding-fake-to-system-under-test.test.js} | 0 ...kes-2-using-fakes-instead-of-stubs.test.js | 21 +++++++++++++++++++ ...akes-3-creating-without-behaviour.test.js} | 5 ++--- ...-4-creating-with-custom-behaviour.test.js} | 0 ...eturns.test.js => fakes-5-returns.test.js} | 0 ...-throws.test.js => fakes-6-throws.test.js} | 2 +- ...-yields.test.js => fakes-7-yields.test.js} | 2 ++ ...c.test.js => fakes-8-yields-async.test.js} | 2 ++ ...lback.test.js => fakes-9-callback.test.js} | 1 + docs/release-source/release/fakes.md | 5 ++--- 13 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 docs/release-source/release/examples/fakes-1-using-fakes-instead-of-spies.test.js rename docs/release-source/release/examples/{fakes-8-firstArg.test.js => fakes-10-firstArg.test.js} (100%) rename docs/release-source/release/examples/{fakes-10-adding-fake-to-system-under-test.test.js => fakes-11-lastArg.test.js} (95%) rename docs/release-source/release/examples/{fakes-9-lastArg.test.js => fakes-12-adding-fake-to-system-under-test.test.js} (100%) create mode 100644 docs/release-source/release/examples/fakes-2-using-fakes-instead-of-stubs.test.js rename docs/release-source/release/examples/{fakes-1-creating-without-behaviour.test.js => fakes-3-creating-without-behaviour.test.js} (71%) rename docs/release-source/release/examples/{fakes-2-creating-with-custom-behaviour.test.js => fakes-4-creating-with-custom-behaviour.test.js} (100%) rename docs/release-source/release/examples/{fakes-3-returns.test.js => fakes-5-returns.test.js} (100%) rename docs/release-source/release/examples/{fakes-4-throws.test.js => fakes-6-throws.test.js} (86%) rename docs/release-source/release/examples/{fakes-5-yields.test.js => fakes-7-yields.test.js} (82%) rename docs/release-source/release/examples/{fakes-6-yields-async.test.js => fakes-8-yields-async.test.js} (82%) rename docs/release-source/release/examples/{fakes-7-callback.test.js => fakes-9-callback.test.js} (95%) diff --git a/docs/release-source/release/examples/fakes-1-using-fakes-instead-of-spies.test.js b/docs/release-source/release/examples/fakes-1-using-fakes-instead-of-spies.test.js new file mode 100644 index 000000000..ee495dbca --- /dev/null +++ b/docs/release-source/release/examples/fakes-1-using-fakes-instead-of-spies.test.js @@ -0,0 +1,17 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should be able to be used instead of spies", function () { + const foo = { + bar: () => "baz", + }; + // wrap existing method without changing its behaviour + const fake = sinon.replace(foo, "bar", sinon.fake(foo.bar)); + + assert.equals(fake(), "baz"); // behaviour is the same + assert.equals(fake.callCount, 1); // calling information is saved + }); +}); diff --git a/docs/release-source/release/examples/fakes-8-firstArg.test.js b/docs/release-source/release/examples/fakes-10-firstArg.test.js similarity index 100% rename from docs/release-source/release/examples/fakes-8-firstArg.test.js rename to docs/release-source/release/examples/fakes-10-firstArg.test.js diff --git a/docs/release-source/release/examples/fakes-10-adding-fake-to-system-under-test.test.js b/docs/release-source/release/examples/fakes-11-lastArg.test.js similarity index 95% rename from docs/release-source/release/examples/fakes-10-adding-fake-to-system-under-test.test.js rename to docs/release-source/release/examples/fakes-11-lastArg.test.js index d8c121558..5cb877413 100644 --- a/docs/release-source/release/examples/fakes-10-adding-fake-to-system-under-test.test.js +++ b/docs/release-source/release/examples/fakes-11-lastArg.test.js @@ -13,6 +13,7 @@ describe("FakeTest", function () { f(1, 2, date2); assert.isTrue(f.lastArg === date2); + // spy call methods: assert.isTrue(f.getCall(0).lastArg === date1); assert.isTrue(f.getCall(1).lastArg === date2); assert.isTrue(f.lastCall.lastArg === date2); diff --git a/docs/release-source/release/examples/fakes-9-lastArg.test.js b/docs/release-source/release/examples/fakes-12-adding-fake-to-system-under-test.test.js similarity index 100% rename from docs/release-source/release/examples/fakes-9-lastArg.test.js rename to docs/release-source/release/examples/fakes-12-adding-fake-to-system-under-test.test.js diff --git a/docs/release-source/release/examples/fakes-2-using-fakes-instead-of-stubs.test.js b/docs/release-source/release/examples/fakes-2-using-fakes-instead-of-stubs.test.js new file mode 100644 index 000000000..04a272daf --- /dev/null +++ b/docs/release-source/release/examples/fakes-2-using-fakes-instead-of-stubs.test.js @@ -0,0 +1,21 @@ +require("@fatso83/mini-mocha").install(); +const sinon = require("sinon"); +const referee = require("@sinonjs/referee"); +const assert = referee.assert; + +describe("FakeTest", function () { + it("should be able to be used instead of stubs", function () { + const foo = { + bar: () => "baz", + }; + // replace method with a fake one + const fake = sinon.replace( + foo, + "bar", + sinon.fake.returns("fake value") + ); + + assert.equals(fake(), "fake value"); // returns fake value + assert.equals(fake.callCount, 1); // saves calling information + }); +}); diff --git a/docs/release-source/release/examples/fakes-1-creating-without-behaviour.test.js b/docs/release-source/release/examples/fakes-3-creating-without-behaviour.test.js similarity index 71% rename from docs/release-source/release/examples/fakes-1-creating-without-behaviour.test.js rename to docs/release-source/release/examples/fakes-3-creating-without-behaviour.test.js index 1164a1d30..51879e0df 100644 --- a/docs/release-source/release/examples/fakes-1-creating-without-behaviour.test.js +++ b/docs/release-source/release/examples/fakes-3-creating-without-behaviour.test.js @@ -8,8 +8,7 @@ describe("FakeTest", function () { // create a basic fake, with no behavior const fake = sinon.fake(); - assert.isUndefined(fake()); - - assert.equals(fake.callCount, 1); + assert.isUndefined(fake()); // by default returns undefined + assert.equals(fake.callCount, 1); // saves call information }); }); diff --git a/docs/release-source/release/examples/fakes-2-creating-with-custom-behaviour.test.js b/docs/release-source/release/examples/fakes-4-creating-with-custom-behaviour.test.js similarity index 100% rename from docs/release-source/release/examples/fakes-2-creating-with-custom-behaviour.test.js rename to docs/release-source/release/examples/fakes-4-creating-with-custom-behaviour.test.js diff --git a/docs/release-source/release/examples/fakes-3-returns.test.js b/docs/release-source/release/examples/fakes-5-returns.test.js similarity index 100% rename from docs/release-source/release/examples/fakes-3-returns.test.js rename to docs/release-source/release/examples/fakes-5-returns.test.js diff --git a/docs/release-source/release/examples/fakes-4-throws.test.js b/docs/release-source/release/examples/fakes-6-throws.test.js similarity index 86% rename from docs/release-source/release/examples/fakes-4-throws.test.js rename to docs/release-source/release/examples/fakes-6-throws.test.js index ec42e9576..a92c81edc 100644 --- a/docs/release-source/release/examples/fakes-4-throws.test.js +++ b/docs/release-source/release/examples/fakes-6-throws.test.js @@ -7,7 +7,7 @@ describe("FakeTest", function () { it("should create a fake that 'throws' an Error", function () { const fake = sinon.fake.throws(new Error("not apple pie")); - // Expect to throw an error with message 'not apple pie' + // Expected to throw an error with message 'not apple pie' assert.exception(fake, { name: "Error", message: "not apple pie" }); }); }); diff --git a/docs/release-source/release/examples/fakes-5-yields.test.js b/docs/release-source/release/examples/fakes-7-yields.test.js similarity index 82% rename from docs/release-source/release/examples/fakes-5-yields.test.js rename to docs/release-source/release/examples/fakes-7-yields.test.js index 639d1c277..70b7aa3e8 100644 --- a/docs/release-source/release/examples/fakes-5-yields.test.js +++ b/docs/release-source/release/examples/fakes-7-yields.test.js @@ -11,8 +11,10 @@ describe("FakeTest", function () { sinon.replace(fs, "readFile", fake); fs.readFile("somefile", (err, data) => { + // called with fake values given to yields as arguments assert.isNull(err); assert.equals(data, "file content"); + // since yields is synchronous, anotherFake is not called yet assert.isFalse(anotherFake.called); }); diff --git a/docs/release-source/release/examples/fakes-6-yields-async.test.js b/docs/release-source/release/examples/fakes-8-yields-async.test.js similarity index 82% rename from docs/release-source/release/examples/fakes-6-yields-async.test.js rename to docs/release-source/release/examples/fakes-8-yields-async.test.js index 6012c416c..1c8d16822 100644 --- a/docs/release-source/release/examples/fakes-6-yields-async.test.js +++ b/docs/release-source/release/examples/fakes-8-yields-async.test.js @@ -11,8 +11,10 @@ describe("FakeTest", function () { sinon.replace(fs, "readFile", fake); fs.readFile("somefile", (err, data) => { + // called with fake values given to yields as arguments assert.isNull(err); assert.equals(data, "file content"); + // since yields is asynchronous, anotherFake is called first assert.isTrue(anotherFake.called); }); diff --git a/docs/release-source/release/examples/fakes-7-callback.test.js b/docs/release-source/release/examples/fakes-9-callback.test.js similarity index 95% rename from docs/release-source/release/examples/fakes-7-callback.test.js rename to docs/release-source/release/examples/fakes-9-callback.test.js index d22bdef56..129c30ea1 100644 --- a/docs/release-source/release/examples/fakes-7-callback.test.js +++ b/docs/release-source/release/examples/fakes-9-callback.test.js @@ -13,6 +13,7 @@ describe("FakeTest", function () { f(1, 2, 3, cb2); assert.isTrue(f.callback === cb2); + // spy call methods: assert.isTrue(f.getCall(1).callback === cb2); assert.isTrue(f.lastCall.callback === cb2); }); diff --git a/docs/release-source/release/fakes.md b/docs/release-source/release/fakes.md index db604ec85..bd365287f 100644 --- a/docs/release-source/release/fakes.md +++ b/docs/release-source/release/fakes.md @@ -6,7 +6,7 @@ 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]. +`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). The available [behaviors](#fakes-with-behavior) for the most part match the API of 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. @@ -167,8 +167,7 @@ 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 following properties are just a few of them, you can refer to -[spies][spies] documentation for all of them. +The instance properties are the same as those of a [`sinon.spy`][spies]. The following examples showcase just a few of the properties available to you. Refer to the [spy docs][spies] for a complete list. #### `f.callback`