Skip to content

Instantly share code, notes, and snippets.

@juanghurtado
Created June 5, 2012 17:30
Show Gist options
  • Save juanghurtado/2876409 to your computer and use it in GitHub Desktop.
Save juanghurtado/2876409 to your computer and use it in GitHub Desktop.
Jasmine Sinon Backbone
define(
[
"backbone", "marionette", "jquery",
"modules/app/app",
"text!modules/index/templates/weather.html",
"text!modules/index/templates/multiple.html",
"text!modules/index/templates/loading.html"
],
function(Backbone, Marionette, $, App, weatherTpl, multipleTpl, loadingTpl) {
var module = App.module('Index');
// Views and Model definitions....
// This callback is executed when "Module.start()"
module.addInitializer(function() {
// Put loading view under App main region
var loadingView = new module.LoadingView();
App.mainRegion.show(loadingView);
// Get weather
var weatherModel = new module.WeatherModel({
query : "SP/Sevilla"
});
var weatherView = new module.WeatherView({
model : weatherModel
});
// How can I test this on my spec?
weatherModel.fetch();
});
return module;
}
);
define(
["modules/index/index"],
function(Index) {
describe("Index module", function() {
it("Should fetch WeatherModel on start", function() {
// With this two spies I assert that "new WeatherModel" and "new WeatherView" are called at start of IndexModule
var spyWeatherModel = sinon.spy(Index, "WeatherModel");
var spyWeatherView = sinon.spy(Index, "WeatherView");
// How do I assert that "weatherModel.fetch();" has been called on "Index.start()"? I can't spy on "fetch" because: "Attempted to wrap undefined property fetch as function"
var spyWeatherModelFetch = sinon.spy(Index.WeatherModel, "fetch");
Index.start();
expect(spyWeatherModel).toHaveBeenCalled();
expect(spyWeatherView).toHaveBeenCalled();
expect(spyWeatherModelFetch).toHaveBeenCalled();
});
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment