[英]How do I create a relationship in ember-cli-mirage?
I'm trying to create Relationships in Ember-Cli-Mirage. 我正试图在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: TASKSCORE:
import DS from 'ember-data';
export default DS.Model.extend({
score: DS.attr('number'),
team: DS.belongsTo('team'),
task: DS.belongsTo('task'),
});
Currently you need to assign the ids manually. 目前,您需要手动分配ID。 You could do this in your fixture files, but I prefer to use factories, as they give me a bit more flexibility in my tests.
您可以在fixture文件中执行此操作,但我更喜欢使用工厂,因为它们在我的测试中给了我更多的灵活性。
Define your factories, just assigning the plain attributes: 定义工厂,只需分配普通属性:
// 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}`; },
});
Then use factory overrides to associate your data in your tests: 然后使用工厂覆盖将测试中的数据关联起来:
// tests/acceptance/some-test.js
test('', function() {
let task = server.create('task');
server.createList('team', 5, {task_id: task.id});
});
This creates the related data in your Mirage db. 这将在Mirage数据库中创建相关数据。 Now if your routes are set up to return associated data, you should be all set.
现在,如果您的路由设置为返回关联数据,则应该全部设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.