dimanche 25 janvier 2015

For a front-end test with Jasmine, what are good strategies for re-creating DOM?

I'm currently building a web app in which I bind Backbone views to DOM elements and then check whether they behave as expected.


I test the front-end of this app using Jasmine. To do this, I need to re-create the DOM that's used by the app. I currently see two strategies for doing so:




  1. Add each DOM element as needed to test a specific view, for instance by using something like $('<div id="toddler"></div>').appendTo('body'); or a tool like jasmine-fixture.




  2. Restructure my web app so that I can load all DOM elements at once. For instance, this could be done by writing two scripts: The first loads the DOM elements, and the second binds the views to the DOM elements. I then use these files to both generate the body of my app, and to load the DOM elements for the Jasmine tests (inside Spec.js). This way, one only needs to write the code for the DOM once.




Are there specific reasons to prefer (1) over (2)? In addition, are there other strategies that can help me in this testing effort?


Thanks!


Aucun commentaire:

Enregistrer un commentaire