From a8b4965fb1eca8f53bdaa91a68d53b34307b9966 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Fri, 27 Oct 2017 12:54:28 +0800 Subject: [PATCH] sinon: update --- sinon.md | 142 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 91 insertions(+), 51 deletions(-) diff --git a/sinon.md b/sinon.md index ea8cbc14a..8588a79cc 100644 --- a/sinon.md +++ b/sinon.md @@ -1,90 +1,130 @@ --- title: Sinon category: JavaScript libraries -layout: default-ad +layout: 2017/sheet +weight: -1 +updated: 2017-10-27 --- ### Creating spies - fn = sinon.spy(); +```js +fn = sinon.spy() +fn() +``` - fn(); - fn.calledOnce == true - fn.callCount == 1 +```js +fn.calledOnce == true +fn.callCount == 1 +``` ### Spying/stubbing - sinon.spy($, 'ajax') +```js +sinon.spy($, 'ajax') +``` - $.ajax(); - $.ajax.calledOnce == true +```js +$.ajax(); +$.ajax.calledOnce == true +``` - sinon.stub($, 'ajax', function () { ... }); // function optional +```js +sinon.stub($, 'ajax', function () { ... }) // function optional +``` - $.ajax.calledWithMatch({ url: '/x' }); - $.ajax.restore(); +```js +$.ajax.calledWithMatch({ url: '/x' }) +$.ajax.restore() +``` ### Spy/stub properties - spy - .args //=> [ [..], [..] ] one per call - .thisValues - .returnValues +```js +spy + .args //=> [ [..], [..] ] one per call + .thisValues + .returnValues +``` - .called //=> true - .notCalled - .callCount - .calledOnce - .calledTwice - .calledThrice +```js + .called //=> true + .notCalled + .callCount + .calledOnce + .calledTwice + .calledThrice +``` - .getCalls() //=> Array - .getCall(0) - .firstCall +```js + .getCalls() //=> Array + .getCall(0) + .firstCall +``` ### Anonymous stub - stub = sinon.stub().returns(42); - stub() == 42 +```js +stub = sinon.stub().returns(42) +stub() == 42 +``` - stub - .withArgs(42).returns(1); - .withArgs(43).throws("TypeError"); +```js +stub + .withArgs(42).returns(1) + .withArgs(43).throws("TypeError") +``` - stub - .returns(1); - .throws("TypeError"); - .returnsArg(0); // Return 1st argument - .callsArg(0); +```js +stub + .returns(1) + .throws("TypeError") + .returnsArg(0) // Return 1st argument + .callsArg(0) +``` ### Fake date - sinon.useFakeTimers(+new Date(2011,9,1)); +```js +sinon.useFakeTimers(+new Date(2011,9,1)); +``` ### Fake server - server = sinon.fakeServer.create(); +```js +server = sinon.fakeServer.create() +``` - $.get('/file.json', ...); - server.requests[0].respond( - 200, - { "Content-Type": "application/json" }, - JSON.stringify([{ id: 1, text: "Provide examples", done: true }]) - ); +```js +$.get('/file.json', ...) +server.requests[0].respond( + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify({ hello: 'world' }) +) +``` - server.restore(); +```js +server.restore() +``` ### Fake XHR - xhr = sinon.useFakeXMLHttpRequest(); - xhr.restore(); +```js +xhr = sinon.useFakeXMLHttpRequest() +xhr.restore() +``` ### Sandbox - beforeEach(function() { - global.sinon = require('sinon').sandbox.create(); - }); +```js +beforeEach(function() { + global.sinon = require('sinon').sandbox.create() +}) +``` - afterEach(function() { - global.sinon.restore(); - }); +```js +afterEach(function() { + global.sinon.restore() +}) +```