Wednesday, 15 September 2010

backbone.js - Setting URL in Backbone.Router for QUnit testing -



backbone.js - Setting URL in Backbone.Router for QUnit testing -

i have backbone.router based myrouter, calls set() methods based on url:

var myrouter = backbone.router.extend({ routes: { "dosomething/:value": "dosetteronmodel", }, dosetteronmodel: function(value) { // set(value) calls on model }, });

i test if model gets updated (set()) correctly using qunit test:

test( "update model via url change", function() { var value = "newvalue"; var url = "http://localhost/#/dosomething/" + value; //does not work moves page away qunit's tests.html page window.location = url; // not work because moves away qunit's tests.html page myrouter.navigate(url); deepequal( mymodel.get(key), value, "value on model not updated url change"); });

how can set url in qunit in order test if router performs right action url?

when want test routers bypass document navigation altogether , phone call backbone.history.loadurl, responsible matching url fragments router methods:

backbone.history.loadurl("#/dosomething");

the additional benefit here don't need phone call backbone.history.start in test code. note utilize or relative url. specifying http://localhost part not necessary.

tiny demo here.

backbone.js qunit backbone-routing

No comments:

Post a Comment