I have a view who extends an external library (let’s call it ExtLib) also based on backbone marionette. Basically this lib is a wrapper of common views that share behaviours (e.g. DetailsView, FormView, etc..). So, let’s say I have something like this:
MyApp.module('Views', function(Views, App, Backbone, Marionette, $, _) {
Views.MyDetailsView = Marionette.ExtLib.DetailsView.extend({
options: {
fields: [
// ...
],
myFunction: function() {
// ...
}
}
});
});
In my Jasmine test, I call a controller method that creates an instance of MyDetailsView.
it('does what it should', function() {
// ...
controller.showDetails();
// my expectations
});
Actually, the method in controller just creates the view and render it (into a layout’s region).
showDetails: function(story) {
var container = new App.Layouts.DetailsLayout();
var myDetailsView = new App.Views.MyDetailsView({
model: story
});
container.details.show(storyDetailsView);
}
Finally, here’s the error that i get:
TypeError: Cannot read property 'length' of undefined
at eval (eval at <anonymous> (http://localhost:8080/myapp/js/lib/underscore/underscore-min.js?_debugResources=y&n=1354890505115:30:350), <anonymous>:17:35)
at c (http://localhost:8080/myapp/js/lib/underscore/underscore-min.js?_debugResources=y&n=1354890505115:30:430)
at Object.Backbone.Marionette.Renderer.render (http://localhost:8080/myapp/js/backbone-app/conf/renderer.js?_debugResources=y&n=1354890505115:9:17)
at r.ItemView.r.View.extend.render (http://localhost:8080/myapp/js/lib/backbone/backbone.marionette.min.js?_debugResources=y&n=1354890505115:13:4719)
at Backbone.Marionette.ExtLib.ExtLib.DetailsView.Backbone.Marionette.View.extend.renderFieldView (http://localhost:8080/myapp/js/lib/backbone.marionette.extlib.min.js?_debugResources=y&n=1354890505115:395:14)
// etc...
I think that the problem is that jasmine is not aware of ExtLib. How can I spy on that renderFieldView to avoid the error?
Thanks in advance, as always.
I can’t tell from that stack trace whether your diagnosis is correct; but to verify that, you can view source on the Jasmine runner and see if ExtLib is included in the runner’s page.
If ExtLib is not included, then the fix depends on how you’re running Jasmine. If you’re using the Jasmine gem and you have a jasmine.yml file, you’ll want to edit jasmine.yml to include ExtLib. If you have a “simple” configuration and you are maintaining Jasmine’s included Javascript files by hand, then you’ll need to add a line to include ExtLib in the runner’s source file.