Currently I have the following code which will setup a sinon server and do some other stuff, FakeServer is an AMD module that I can import into my tests. It has a create() and destroy() method:
describe('do something', function() {
var server;
beforeEach(function() {
server = FakeServer.create();
setupRoutes();
});
afterEach( function(){
FakeServer.destroy(server);
});
What I actually want is to automatically add this logic to the beforeEach and afterEach functions (to prevent the user from forgetting to add the afterEach statement).
Something like this:
describe('do something', function() {
var server = FakeServer.init(this);
How can I do this which will automatically setup the beforeEach and afterEach functions to call the fake_server create() and destrow() functions?
fake_server.js
define(function(require) {
require('sinon.server');
"use strict";
function create() {
$.ajaxSetup({
async: false
});
var server = sinon.fakeServer.create();
server.autoRespond = true;
return server;
}
function destroy(server) {
server.restore();
$.ajaxSetup({
async: true
});
}
return {
create : create,
destroy : destroy
}
});
Here is what I came up with. I believe it works, but I do not have the same setup as you. I am posting my solution, which has the modifications that I need to get this to run. I hope it puts on you the right track.
So basically in fake_server init call, I am over riding the implementations for both beforeEach and afterEach to call your server code and then call what ever the original function was to call them with. That will handle if there is additional beforeEach or afterEach setup in the test file.
I was not able to return the server from the init function as it is not initialized until beforeEach is actually called, but that could be changed to. I added a helper function to FakeServer to get the server.
fake_server.js
var FakeServer = (function () {
//require('sinon.server');
"use strict";
var myServer;
function create() {
$.ajaxSetup({
async: false
});
var server = {}; //sinon.fakeServer.create();
server.autoRespond = true;
return server;
}
function destroy(server) {
//server.restore();
$.ajaxSetup({
async: true
});
}
function init(describeFunc) {
// grab the original beforeEach and afterEach calles
var beforeEach = describeFunc.beforeEach;
var afterEach = describeFunc.afterEach;
// replace beforeEach
describeFunc.beforeEach = function (fn) {
// call the original beforeEach, in the correct context
beforeEach.call(describeFunc, function () {
// create the server
myServer = create();
// call what ever was passed into beforeEach
fn();
});
};
describeFunc.afterEach = function (fn) {
afterEach.call(describeFunc, function () {
destroy(myServer);
fn();
});
};
}
return {
create: create,
destroy: destroy,
init: init,
server: function () { return myServer; }
}
})();
testFile.js
/// <reference path="fakeServer.js" />
describe("testing123", function () {
// added for testing only
var beforeEachCalled, afterEachCalled;
FakeServer.init(this);
beforeEach(function () {
// added for testing only
beforeEachCalled = true;
});
afterEach(function () {
// added for testing only
afterEachCalled = true;
});
it("should work", function () {
// to access the server
var s = FakeServer.server();
});
});