Jacob Wellington
Jacob Wellington

Reputation: 41

Testing ember using ember-cli and factory guy

I'm running into a problem where the Ember application I'm testing doesn't seem to be noticing the models that I'm creating with FactoryGuy. Here's my test file:

import Ember from 'ember';
import startApp from '../helpers/start-app';
import FactoryGuy from 'factory-guy';
import { testMixin as FactoryGuyTestMixin} from 'factory-guy';
import carsFactory from "../fixtures/car";

var application, testHelper, store, make;
var TestHelper = Ember.Object.createWithMixins(FactoryGuyTestMixin);

module('Acceptance: Cars', {
    setup: function() {
        application = startApp();
        testHelper = TestHelper.setup(application);
        store = testHelper.getStore();
        testHelper.make('car');
    },
    teardown: function() {
        Ember.run(function() { testHelper.teardown(); });
        Ember.run(application, 'destroy');
    }
});

test('visiting /cars', function() {
    equal(store.all('car').get('content.length'), 1);
    visit('/cars');

    andThen(function() {
        equal(currentPath(), 'cars');
        var li = find('li');
        equal(li.length, 2);
    });
});

The first and second equal assertions will succeed, but the last one will fail. Here's what my template looks like:

<ul>
{{#each car in model}}
<li>{{car.label}}</li>
{{/each}}
</ul>

And my route:

import Ember from 'ember';

export default Ember.Route.extend({
    model: function () {
        this.store.find('car');
    }
});

What am I missing in getting the Ember app's store to get properly populated by the FactoryGuy's make method?

Edit: I also have tried adding the following line at the top of the test method and in the setup function, and it still isn't working correctly.

testHelper.handleFindMany('car', 1);

Upvotes: 1

Views: 617

Answers (1)

DanielSpaniel
DanielSpaniel

Reputation: 171

EmberDataFactoryGuy is now an ember addon, so if you are using that then the test would look like this:

import Ember from 'ember';
import startApp from '../helpers/start-app';  
import { make } from 'ember-data-factory-guy';
import TestHelper from 'ember-data-factory-guy/factory-guy-test-helper';

var App;

module('Acceptance: Cars', {
  setup: function() {
    Ember.run(function () {
      App = startApp();
      TestHelper.setup();
    });
  },
  teardown: function() {
    Ember.run(function() { 
      TestHelper.teardown();
      App.destroy();
    });
  }
});

test('visiting /cars', function() {
  TestHelper.handleFindAll('car', 2);
  visit('/cars');

  andThen(function() {
    equal(currentPath(), 'cars');
    var li = find('li');
    equal(li.length, 2);
  });

});

There is a sample acceptance test just like this one in the ember-data-factory-guy repo here ( looks pretty much just like this one though ):

https://github.com/danielspaniel/ember-data-factory-guy/blob/master/tests/acceptance/users-view-test.js

Anyway, there is no more hassle of setting the store, or creating TestHelper, it's all done for you, and setup automatically when you start the application.

Upvotes: 1

Related Questions