Using FactoryGuy for Ember tests

When we upgraded from ember v1.7.0 to v1.8.1 and from ember-data v1.0.0-beta.9 to v1.0.0-beta.12 we noticed that ember became very strict in the expected json. That's why not only we had to update the sideloading in our api, but also we had to adjust our test fixtures.

This became a problem since we had very long fixtures and it became hard to control the data. That's why we switch to FactoryGuy.

In our old situation the fixtures were used like this:

Fixtures and Pretender

Here on the left side you see an example of the fixtures we use. The right side shows how we use Pretender to mock CRUD requests with these fixtures.

With FactoryGuy we only have to define the models that we use in the appliction as factories. Factory guy let us mock the store and gives functions to make objects from the defined factories and put them in the store.

On the left side you see the factory that corresponds with the fixture showed above, and on the right side is the implementation of the factory in the application.

For the create, update and delete requests made in the application, we also stop using our fixtures and MockResponses. FactoryGuy uses jquery-mockjax for this and implemented it in it's library. With this we can now do:

test('save chapter without confirm', function () {  
  expect(1);
  var newName = 'a new name'
  var learningSpace = make('learningSpace')
  visit('/spaces/' + learningSpace.id + '/chapters/new');
  fillIn('#entry-title input', newName);
  fillIn('.editor-markdown .ember-text-area', 'markdown text');
  testHelper.handleCreate('chapter', { name: newName });
  click('#entry-options .button-save');

    andThen(function () {
      var actual = currentRouteName();
      var expected = 'chapter.index';
      equal(actual, expected, 'expected ' + expected + ' but got ' + actual);
    });
});

Notice testHelper.handleCreate('chapter', { name: newName }); which mocks the create request for a chapter.

If you have any questions ask us on Twitter