Skip to content

Instantly share code, notes, and snippets.

@hokuma
Last active October 18, 2015 12:35
Show Gist options
  • Save hokuma/f9c1f54733a661477650 to your computer and use it in GitHub Desktop.
Save hokuma/f9c1f54733a661477650 to your computer and use it in GitHub Desktop.
node環境でsinon.jsのfakeServerを使う ref: http://qiita.com/halhide/items/498f094ac670884e55c9
// ブラウザ環境っぽいものを作る
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);
mocha --compilers js:babel/register test.js
// 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