Last active
October 18, 2015 12:35
-
-
Save hokuma/f9c1f54733a661477650 to your computer and use it in GitHub Desktop.
node環境でsinon.jsのfakeServerを使う ref: http://qiita.com/halhide/items/498f094ac670884e55c9
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
// ブラウザ環境っぽいものを作る | |
import jsdom from 'jsdom'; | |
import jQuery from 'jquery'; | |
import { XMLHttpRequest } from 'w3c-xmlhttprequest'; | |
global.XMLHttpRequest = XMLHttpRequest; | |
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>'); | |
global.window = document.parentWindow; | |
global.$ = jQuery(window); |
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
mocha --compilers js:babel/register test.js |
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
// chai使ってます | |
import './browser'; | |
import chai, { expect } from 'chai'; | |
import sinon from 'sinon'; | |
import sinonChai from 'sinon-chai'; | |
chai.use(sinonChai); | |
describe('FakeServer in node.js', () => { | |
let server; | |
let spy; | |
beforeEach(() => { | |
server = sinon.fakeServer.create(); | |
spy = sinon.spy(); | |
}); | |
afterEach(() => { | |
server.restore(); | |
}); | |
it('fake responseが返ること', () => { | |
server.respondWith('GET', '/sample.json', [ | |
200, | |
{ 'Content-Type': 'application/json' }, | |
JSON.stringify({message: 'Hello World'}) | |
]); | |
$.getJSON('/sample.json') | |
.then((data) => { | |
spy(data); | |
}); | |
server.respond(); | |
expect(spy).to.have.been.calledWith({message: 'Hello World'}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment