Organizing Fixtures  page     

The best way of organizing fixtures is to have a 'fixtures.js' file that steals jquery/dom/fixture and defines all your fixtures. For example, if you have a 'todo' application, you might have todo/fixtures/fixtures.js look like:

steal({
        path: '//jquery/dom/fixture.js',
        ignore: true
      })
      .then(function(){

  $.fixture({
      type: 'get',  
      url: '/services/todos.json'
    },
    '//todo/fixtures/todos.json');

  $.fixture({
      type: 'post',  
      url: '/services/todos.json'
    },
    function(settings){
        return {id: Math.random(), 
             name: settings.data.name}
    });

})

Notice: We used steal's ignore option to prevent loading the fixture plugin in production.

Finally, we steal todo/fixtures/fixtures.js in the app file (todo/todo.js) like:

steal({path: '//todo/fixtures/fixtures.js',ignore: true});

//start of your app's steals
steal.plugins( ... )

We typically keep it a one liner so it's easy to comment out.

Switching Between Sets of Fixtures

If you are using fixtures for testing, you often want to use different sets of fixtures. You can add something like the following to your fixtures.js file:

if( /fixtureSet1/.test( window.location.search) ){
  $.fixture("/foo","//foo/fixtures/foo1.json');
} else if(/fixtureSet2/.test( window.location.search)){
  $.fixture("/foo","//foo/fixtures/foo1.json');
} else {
  // default fixtures (maybe no fixtures)
}
© Jupiter IT - JavaScriptMVC Training and Support