public
Last active

Mixing Require.js, Jasmine, Sinon.js and Backbone.

  • Download Gist
DefineSpecsAsModules.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
## ItemView.coffee in the specs/coffee folder ##
 
define ["ItemView"], (ItemView) ->
###
Specs for ItemView
###
describe "Item View", ->
 
###
Common setup and teardown
###
beforeEach ->
# Make a new arbitraty Backbone.Model
@model = new Backbone.Model
id: 1
title: "Sample Todo"
priority: 2
done: false
 
# And make a view for it.
@view = new ItemView model: @model
 
###
Rendering tests
###
describe "Rendering", ->
 
###
Common setup and teardown
###
beforeEach ->
@view.render()
@el = $ @view.el
 
it "returns the view object", ->
# Check that the render method returns the view
expect( @view.render() ).toEqual @view
GlobalizeClasses.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11
require ["model","collection","view","router","etc"], (main,collection,view,router,etc) ->
# Globalize the classes
window.main = main
window.collection = collection
window.view = view
window.router = router
window.etc = etc
# And then run the specs
# ...
jasmineEnv.execute()
# ...
LoadSpecModulesInRunerModule.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
## SpecRunner.js in the specs/js folder ##
define( [
"specs/Item",
"specs/List",
"specs/ItemView",
"specs/ListView",
"specs/Routes",
"specs/MainRouter",
], function(){
 
return SpecRunner = function() {
 
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
 
var trivialReporter = new jasmine.TrivialReporter();
 
jasmineEnv.addReporter(trivialReporter);
 
jasmineEnv.specFilter = function(spec) {
return trivialReporter.specFilter(spec);
};
 
return jasmineEnv.execute();
}
}
);
LoadSpecRunner.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// SpecRunner.html //
// ...
$(document).ready( function() {
 
window.App = {}
 
// Regular Require.js configuration here.
require.config({
baseUrl: '../build/js', // Leave the baseUrl as in your release build
paths: {
text: 'libs/require/text',
specs: '../../specs/js' // Adding this helps a lot...
}
});
 
require([
'specs/SpecRunner', // ...as shown here.
],
function(SpecRunner) {
SpecRunner();
}
);
 
});
// ...
RequireInIt.coffee
CoffeeScript
1 2 3 4
describe "something", ->
it "something else", ->
require ["dep1","dep2","..."], (dep1,dep2,...)->
#expectation

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.