6

I'm trying to create Relationships in Ember-Cli-Mirage. Is this possible in a simple way or do I have to use fixtures instead of factories?

These would be my models:

TASK:

export default DS.Model.extend({
  taskName: DS.attr('string'),
  team: DS.hasMany('team', {async: true}),
  taskScore: DS.hasMany('taskScore', {async: true})
});

TEAM:

import DS from 'ember-data';

export default DS.Model.extend({
  teamName: DS.attr('string'),
  task: DS.hasMany('task'),
  taskScore: DS.hasMany('taskScore', {async: true})
});

TASKSCORE:

import DS from 'ember-data';

export default DS.Model.extend({
    score: DS.attr('number'),
    team: DS.belongsTo('team'),
    task: DS.belongsTo('task'),
});
4

1 回答 1

6

目前您需要手动分配 ID。您可以在您的夹具文件中执行此操作,但我更喜欢使用工厂,因为它们在我的测试中给了我更多的灵活性。

定义您的工厂,只需分配普通属性:

// app/mirage/factories/task.js
export default Mirage.Factory.extend({
  task_name(i) { return `Task ${i}`; },
});

// app/mirage/factories/team.js
export default Mirage.Factory.extend({
  team_name(i) { return `Task ${i}`; },
});

然后使用工厂覆盖在测试中关联您的数据:

// tests/acceptance/some-test.js

test('', function() {
  let task = server.create('task');
  server.createList('team', 5, {task_id: task.id});
});

这会在您的 Mirage 数据库中创建相关数据。现在,如果您的路线已设置为返回相关数据,那么您应该已准备就绪。

于 2015-08-01T13:46:34.327 回答