Ember.js:如何集成测试与余烬数据模型交互的组件
我正在建立一个相对简单的评论列表
组件。我想传递可评论模型(例如 Post
),并让组件负责创建,编辑,删除评论。现在,我绕过所有各种动作,而且非常脆弱。
I'm building a relatively straight-foward comment-list
component. I want to pass in the commentable model (say a Post
) and have the component take care of creating, editing, deleting comments. Right now I pass around all the various actions and it's been extremely brittle.
如何在组件集成测试中创建Ember Data模型的真实实例?
How do I create a true instance of an Ember Data model in a component integration test?
我的直属原以为是要导入模型,然后 .create({})
导入模型,但是的错误改用this.store.createRecord()
My immediate thought was to import the model then .create({})
it but that errors with use this.store.createRecord() instead
/* jshint expr:true */
import { assert } from 'chai';
import { describeComponent, it } from 'ember-mocha';
import hbs from 'htmlbars-inline-precompile';
import Post from 'ownersup-client/post/model';
describeComponent( 'comment-list', 'Integration: CommentListComponent', {
integration: true
},
function() {
it('renders all of the comments', function() {
const model = Post.create({ title: 'title' });
model.get('comments').createRecord({ body: 'One Comment' })
this.render(hbs`{{comment-list model=model}}`);
assert.lengthOf(this.$('.comment-list-item'), 1);
});
}
);
有人有什么想法吗?
为与集成测试中的注入相关的类似问题而苦恼的人们的一般答案。
一切都取决于
使用 module
$ b $进行集成测试时b( import {module} from'ember-qunit';
)
When you have an integration test with module
(import { module } from 'ember-qunit';
)
您可以使用 this.owner.lookup('service:store')
在测试中
有关更多信息,Dockyard中有一篇很棒的文章b $ b https://dockyard.com/blog/2018/01 / 11 / modern-ember-testing
for more information, there is a great article from Dockyard https://dockyard.com/blog/2018/01/11/modern-ember-testing