根据版本的博客文章,商店现在可以用作服务:ember-data
1.0.0-beta.16
TweetComposerComponent = Ember.Component.extend({
store: Ember.inject.service()
});
但是,我不知道如何qunit
对这样的组件进行单元测试。我尝试了以下方法:
moduleForComponent('tweet-composer', {
needs: ['service:store']
});
和:
moduleForComponent('tweet-composer', {
needs: ['store:main']
});
当我做前者时,我会得到一个错误Attempting to register an unknown factory: 'service:store'
,如果我做后者,那store
就是undefined
.
想法?
(我正在写一个ember-cli
风格的应用程序)。
更新:
在 ember-test-helpers repo 中似乎有一个未解决的问题。
在等待此修复程序时,我制作了一个可以作为权宜之计的助手(coffeescript):
`import TestModuleForComponent from 'ember-test-helpers/test-module-for-component'`
`import { createModule } from 'ember-qunit/qunit-module'`
# This assumes the last argument, the callbacks, is present, although it
# does support the description being an optional argument.
moduleForStoreComponent = ->
args = Array.prototype.slice.call arguments
callbacks = args[args.length-1]
# Wrap the original beforeEach callback in a modified version that
# also sets up the store for the test container.
originalSetup = callbacks.beforeEach
callbacks.beforeEach = ->
DS._setupContainer(@container)
originalSetup.call(@) if originalSetup
callbacks.store = ->
@container.lookup('store:main')
args.unshift TestModuleForComponent
createModule.apply @, args
`export default moduleForStoreComponent`