Skip to content

Instantly share code, notes, and snippets.

@me97esn
Created August 27, 2013 13:47
Show Gist options
  • Save me97esn/6353749 to your computer and use it in GitHub Desktop.
Save me97esn/6353749 to your computer and use it in GitHub Desktop.
An example on how to run a Jasmine test when the code under test is using require.js
define([
'../../../vendor/Squire',
'sinon'
],
function(
Squire, sinon
) {
describe("AppRouter routes", function() {
// set up the async spec
var async = new AsyncSpec(this);
//app is imported in router with require. I want to mock it and verify that the correct method,
//'displayIndex', gets called
var appMock = {
displayIndex: sinon.spy()
};
// run an async setup
async.beforeEach(function(done) {
// Use Squire.js to mock dependencies when using require.js
var injector = new Squire()
.mock('app', appMock) // When 'app' is required in the router.js, use appMock
.require(['router'], function(Router) { // Require the router, the actual code under test
this.router = new Router;
this.router.index(); // Call the index method. TODO: this should be done using router.navigate('') instead, but by some reason it doesn't work
done(); // Tell Jasmine.Async that everything is done
});
});
// run an async expectation
async.it("should call the displayIndex method", function(done) {
expect(appMock.displayIndex.calledOnce).toBe(true); // This expectation will be run after the beforeEach is completed.
// all async calls completed
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment