使用ember-cli和工厂人测试余烬

时间:2015-01-14 17:54:29

标签: ember.js ember-data ember-cli

我遇到了一个问题,我测试的Ember应用程序似乎没有注意到我用FactoryGuy创建的模型。这是我的测试文件:

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);
    });
});

第一个和第二个相等的断言将成功,但最后一个断言将失败。这是我的模板的样子:

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

我的路线:

import Ember from 'ember';

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

让Ember应用程序的商店通过FactoryGuy的make方法正确填充,我缺少什么?

编辑:我也尝试在测试方法的顶部和设置功能中添加以下行,但它仍然无法正常工作。

testHelper.handleFindMany('car', 1);

1 个答案:

答案 0 :(得分:1)

EmberDataFactoryGuy现在是一个ember插件,所以如果您使用它,那么测试将如下所示:

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);
  });

});

这里有一个样本验收测试,就像这里的ember-data-factory-guy repo一样(虽然看起来很像这个):

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

无论如何,没有设置商店或创建TestHelper的麻烦,它已全部为您完成,并在您启动应用程序时自动设置。