backbone.js - Manage global Backbone events while writing Qunit unit tests -
i writing unit tests backbone app. tests trigger events, causing interference among different tests.
here tests
test('user setting company should update departmentslists url', function() { var acme = new company({ id:274, name: "acme solutions" }); var companies = new companylist; var departments = new departmentlist; new companyselectorview({ el: '#company-input', collection: companies }); events.trigger('userset:company', acme); equal(_.result(departments, 'url'), 'http://'+document.location.host+'/data/companies/274/departments'); }); asynctest('user setting company should retrieve companys departments', function() { var acme = new company({ id:274, name: "acme solutions" }); var companies = new companylist; var departments = new departmentlist; new companyselectorview({ el: '#company-input', collection: companies }); events.trigger('userset:company', acme); events.on('fetched:departments', function(response) { deepequal(response, [{id: "8",name: "accounting"},{id: "1",name: "client services"},{id: "470",name: "systems"},{id: "1187",name: "managers"}]); start(); }) });
and relevant part of collection:
var departmentlist = backbone.collection.extend({ initialize: function() { var self = this; events.on("userset:company", function(company) { self.selectedcompany = company; self.fetch({ success: function(collection, response, options) { events.trigger("fetched:departments", response); } }); }); }, model: department, selectedcompany: '', url: function() { return 'http://'+document.location.host+'/data/companies/'+this.selectedcompany.id+'/departments'; } });
what's solution here? want split these 2 tests out each other, since they're different things, want event trigger included in test.
ps: i'm new backbone & unit testing, criticism more welcome.
the simple way solve use events.once
instead of events.on
. way events cleaned after each test.
Comments
Post a Comment