Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Approach to Test Backbone Views with Templates Using Jasmine

I am learning backbone so I am building a simple Backbone app. I am using jasmine framework to test my backbone app.

As luck would have it, my first jasmine test was for a Backbone View. This is a very simple view that uses a haml-js template. App users rails 3.1 so the whole thing is packaged via sprockets with the help of haml-sprockets gem, and it works.

However when I test with jasmine I get an error

TypeError: Cannot call method 'backbone/templates/admin_personal_attribute_template' of undefined

I understand this is because haml-js template is not present.

Normally this template is accessed like this:

$(this.el).html(window.JST["backbone/templates/admin_personal_attribute_template"]({pars:this.model.attributes}));

(i.e. haml-sprockets gem compiles is using haml-js and appends to window.JST).

I am looking for the 'best', i.e. least disruptive and, hopefully, simple, way to test my views + templates in jasmine.

I have found a number of ways to do it, but not sure as what is the 'best':

  1. Use jasmine-headless-webkit . This will utulize sprockets, but loose ability to access jasmine server, so simple but some loss of flexibility
  2. Using phantom.js as desribed by japhr (sorry stackoverflow prevented me from posting more links) will work around the server-side limitations, but this seems to be a very new and untested method, so flexible but not simple
  3. Pre-generate your templates as desribed here. This is conceptually simple, but require some maintenance - so some coding work

Assuming I have a project with ~ 20 models and ~ 40 views and templates, what is the best way to go?

like image 821
user1178907 Avatar asked Jan 30 '12 20:01

user1178907


1 Answers

Check this article: Unit Testing Backbone Applications With Jasmine

It has some examples how to implement unit tests for Backbone JS view

Also there is an O'Reilly book "Developing Backbone.js Applications" which has a whole chapter on testing views with Jasmine and qUnit

like image 164
Maksym Kozlenko Avatar answered Nov 03 '22 08:11

Maksym Kozlenko