测试时初始化的依赖关系不存在 [英] Initialized dependency not present when testing
问题描述
我不想手动注入它来进行测试。有更好的方法来处理吗?
初始化程序:
var FooServiceInitializer = {
name:'foo',
initialize:function(container,application){
application.inject('component:foo','foo ','service:foo');
}
};
导出默认FooServiceInitializer;
失败测试:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b component = this.subject({});
},
teardown:function(){
Ember.run(App,App.destroy);
}
});
test('Properties:foo',function(){
//确保我们注入了服务
ok(component.foo,'foo is inject');
});
正如我之前所说,它真的适合于您在此时测试容器(而不是由ic-ajax创建的迷你容器)进行集成测试。
您的实际测试符合
test(root lists 3 colors,function(){
var c = App .__ container __。lookup :foo-bar');
ok(c.foo.blah);
});
如果您在测试期间使用容器感到内疚(您不应该),您可以创建一个帮助者,以避免在/将来api发生变化时,将其全部修复。
Ember.Test.registerHelper ('containerLookup',
function(app,look){
return app .__ container __。lookup(look);
}
);
确保您在
App.injectTestHelpers();
然后你的测试看起来像
test(root list 3 colors,function(){
var c = containerLookup('component:foo-bar');
ok(c.foo .blah);
});
http://emberjs.jsbin.com/doxigu/edit
I'm using ember-cli 0.0.35, and injecting a dependency onto my component via an initializer. It works great in development, but the property isn't present when I run tests. It appears that testing calls loadInitializers, but the dependency is not showing up on this.subject({});
I don't want to manually inject it for the tests. Is there a better way to handle this?
Initializer:
var FooServiceInitializer = {
name: 'foo',
initialize: function (container, application) {
application.inject('component:foo', 'foo', 'service:foo');
}
};
export default FooServiceInitializer;
Failing Test:
moduleForComponent('bar', 'Component: Bar', {
setup: function() {
App = startApp();
component = this.subject({});
},
teardown: function () {
Ember.run(App, App.destroy);
}
});
test('Properties: foo', function() {
// Make sure we injected the service
ok(component.foo, 'foo is injected');
});
As I said before, it really lends itself to an integration test since you are testing the container at this point (and not the mini container created by ic-ajax).
Your real test is along the lines of this
test("root lists 3 colors", function(){
var c = App.__container__.lookup('component:foo-bar');
ok(c.foo.blah);
});
If you feel guilty about using the container during testing (which you shouldn't) you can create a helper to avoid having to fix it all over the place when/if an api changes in the future.
Ember.Test.registerHelper('containerLookup',
function(app, look) {
return app.__container__.lookup(look);
}
);
Make sure you define this before
App.injectTestHelpers();
and then your test would look like
test("root lists 3 colors", function(){
var c = containerLookup('component:foo-bar');
ok(c.foo.blah);
});
http://emberjs.jsbin.com/doxigu/edit
这篇关于测试时初始化的依赖关系不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!