根据 博客文章 对于 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`