sinon: update

This commit is contained in:
Rico Sta. Cruz 2017-10-27 12:54:28 +08:00
parent 1f98e6a964
commit a8b4965fb1
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 91 additions and 51 deletions

142
sinon.md
View File

@ -1,90 +1,130 @@
--- ---
title: Sinon title: Sinon
category: JavaScript libraries category: JavaScript libraries
layout: default-ad layout: 2017/sheet
weight: -1
updated: 2017-10-27
--- ---
### Creating spies ### Creating spies
fn = sinon.spy(); ```js
fn = sinon.spy()
fn()
```
fn(); ```js
fn.calledOnce == true fn.calledOnce == true
fn.callCount == 1 fn.callCount == 1
```
### Spying/stubbing ### Spying/stubbing
sinon.spy($, 'ajax') ```js
sinon.spy($, 'ajax')
```
$.ajax(); ```js
$.ajax.calledOnce == true $.ajax();
$.ajax.calledOnce == true
```
sinon.stub($, 'ajax', function () { ... }); // function optional ```js
sinon.stub($, 'ajax', function () { ... }) // function optional
```
$.ajax.calledWithMatch({ url: '/x' }); ```js
$.ajax.restore(); $.ajax.calledWithMatch({ url: '/x' })
$.ajax.restore()
```
### Spy/stub properties ### Spy/stub properties
spy ```js
.args //=> [ [..], [..] ] one per call spy
.thisValues .args //=> [ [..], [..] ] one per call
.returnValues .thisValues
.returnValues
```
.called //=> true ```js
.notCalled .called //=> true
.callCount .notCalled
.calledOnce .callCount
.calledTwice .calledOnce
.calledThrice .calledTwice
.calledThrice
```
.getCalls() //=> Array ```js
.getCall(0) .getCalls() //=> Array
.firstCall .getCall(0)
.firstCall
```
### Anonymous stub ### Anonymous stub
stub = sinon.stub().returns(42); ```js
stub() == 42 stub = sinon.stub().returns(42)
stub() == 42
```
stub ```js
.withArgs(42).returns(1); stub
.withArgs(43).throws("TypeError"); .withArgs(42).returns(1)
.withArgs(43).throws("TypeError")
```
stub ```js
.returns(1); stub
.throws("TypeError"); .returns(1)
.returnsArg(0); // Return 1st argument .throws("TypeError")
.callsArg(0); .returnsArg(0) // Return 1st argument
.callsArg(0)
```
### Fake date ### Fake date
sinon.useFakeTimers(+new Date(2011,9,1)); ```js
sinon.useFakeTimers(+new Date(2011,9,1));
```
### Fake server ### Fake server
server = sinon.fakeServer.create(); ```js
server = sinon.fakeServer.create()
```
$.get('/file.json', ...); ```js
server.requests[0].respond( $.get('/file.json', ...)
200, server.requests[0].respond(
{ "Content-Type": "application/json" }, 200,
JSON.stringify([{ id: 1, text: "Provide examples", done: true }]) { 'Content-Type': 'application/json' },
); JSON.stringify({ hello: 'world' })
)
```
server.restore(); ```js
server.restore()
```
### Fake XHR ### Fake XHR
xhr = sinon.useFakeXMLHttpRequest(); ```js
xhr.restore(); xhr = sinon.useFakeXMLHttpRequest()
xhr.restore()
```
### Sandbox ### Sandbox
beforeEach(function() { ```js
global.sinon = require('sinon').sandbox.create(); beforeEach(function() {
}); global.sinon = require('sinon').sandbox.create()
})
```
afterEach(function() { ```js
global.sinon.restore(); afterEach(function() {
}); global.sinon.restore()
})
```