How to use the fakeServer function from sinon
Find comprehensive JavaScript sinon.fakeServer code examples handpicked from public code repositorys.
20 21 22 23 24 25 26 27 28 29
}); }); describe('receiving a github:gist action', () => { let mock = null; beforeEach(() => { mock = sinon.fakeServer.create(); }); afterEach(() => mock.restore()); it('should send application:sethash for existing gist', (done) => { const action = 'github:gist';
1 2 3 4 5 6 7 8 9 10
var sinon = require('sinon'); var tosource = require('tosource'); var Widget = require('../../src/Widget.js'); function makeFakeServer() { var server = sinon.fakeServer.create(); server.respondToRun = function respondToRun(body) { server.respondWith('POST', '/run/', [ status, { 'Content-Type': 'text/plain' },
47 48 49 50 51 52 53 54 55 56
//save xhr currentXhr = global.window.XMLHttpRequest; //delete current xhr delete global.window.XMLHttpRequest; //create new server Server = sinon.fakeServer.create(); //copy over fake xhr window.XMLHttpRequest = Server.xhr; // get a reference to the original onCreate method var onCreate = window.XMLHttpRequest.onCreate;
+ 3 other calls in file
4 5 6 7 8 9 10 11 12 13
var sinon = require('sinon'); var server; before(function () { server = sinon.fakeServer.create(); }); after(function () { server.restore();
+ 7 other calls in file
GitHub: will3/sinon
![will3 profile picture](/will3.png?size=40)
185 186 187 188 189 190 191 192 193 194
The preceding example shows how flexible this API is. If it looks too laborous, you may like the fake server: ```javascript var server; before(function () { server = sinon.fakeServer.create(); }); after(function () { server.restore(); }); it("calls callback with deserialized data", function () { var callback = sinon.fake();
+ 3 other calls in file
7 8 9 10 11 12 13 14 15 16
var defaultConfig = { autoRespond: true, respondImmediately: true }; var server = sinon.fakeServer.create(_.defaults(config, defaultConfig)); var that = { post: method('POST'), get: method('GET'),
GitHub: holidayextras/truman
![holidayextras profile picture](/holidayextras.png?size=40)
102 103 104 105 106 107 108 109 110 111
} return storage.load(fixtureCollectionName) .then((fixtures) => { // Load all of our fixtures into a fake server. const fakeServer = sinon.fakeServer.create() fakeServer.autoRespond = true fakeServer.autoRespondAfter = 0 fakeServer.respondWith(/.*/, (xhr) => { const matchingFixtures = fixtureHelper.findForSinonXHR(fixtures, xhr)
49 50 51 52 53 54 55 56 57 58
}); }); describe.only("when asyncResult has multiple static, dynamic and promised groups", () => { beforeAll(() => { const delayFakeServer = sinon.fakeServer.create(); delayFakeServer.respondWith("GET", /\/group3\.json/, [ 200, { "Content-Type": "application/json" },
36 37 38 39 40 41 42 43 44 45
return this._webGLContext; }; window.useFakeXMLHttpRequest = function() { sinon.xhr.supportsCORS = true; window.server = sinon.fakeServer.create(); window.XMLHttpRequest = window.server.xhr; }; window.URL.revokeObjectURL = function () {};
+ 3 other calls in file
sinon.stub is the most popular function in sinon (5777 examples)