Testing views

Views manage the relationship between data (such as, models or collections) and the user interactions (DOM). In the case of views, you should test for the following:

  • Rendering: Given a model or collection, you should verify that the output HTML is the right one
  • Events: This verifies that the DOM events are handled correctly
  • Model changes: If the model changes something, the view should be in sync

For this example, we are going to test the ContactForm view; the responsibility of this view is to show a form to the user and then get the user input to update a model.

When making test on views, it is recommended to use a fake model and not the original Contact model. The main reason for this is to isolate the ContactView object so that if ...

Get Mastering Backbone.js now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.