在初始化程序中,我将一个对象注入到我的助手中,并且它在应用程序中正常工作。
但是在使用 QUnit 测试助手时,我收到以下错误:
类型错误:未定义不是函数。
帮助器无权访问注入的对象,尽管在App.__container__.lookup('myObject:main')
模块的 setup 函数中调用时,它确实返回了该对象。
我怎样才能使这项工作?测试类基于fiddle1,fiddle2。
CoffeeScript 中的以下示例显示了我的问题:
App = undefined
entered = false
initializedCount = 0
module 'testing',
setup: ->
App = startApp()
teardown: ->
Ember.run(App, 'destroy')
Ember.Application.initializer({
name: "person",
initialize: (container, application) ->
initializedCount++;
person = {
name: "Mary"
}
container.register('person:main', person, {instantiate: false});
container.injection('helper', 'person', 'person:main');
});
createView = (template, context) ->
context = {} unless context
View = Ember.View.extend(
controller: context
template: Ember.Handlebars.compile(template)
)
View.create()
append = (view) ->
Ember.run ->
view.appendTo "#ember-testing"
return
return
Ember.Handlebars.helper "upcase", (value) ->
person = @get('person'); # <-- test fails here
value += person.name;
value.toUpperCase()
Ember.testing = true
test('non-redirect route /third', ->
equal(initializedCount, 2, 'initializer ran');
App.reset();
equal(initializedCount, 3, 'initializer ran');
App.reset();
equal(initializedCount, 4, 'initializer ran');
);
test "a handlebars helper", ->
view = createView("{{upcase 'something'}}")
append view
equal view.$().text(), "SOMETHING MARY"
return