用于单元测试的EmberJS服务注入(Ember QUnit) [英] EmberJS Service Injection for Unit Tests (Ember QUnit)

查看:114
本文介绍了用于单元测试的EmberJS服务注入(Ember QUnit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

规格:




  • Ember版本:1.13.8

  • 节点:0.10.33

  • npm:2.13.4



我有



 


使用别名作为示例,因为我不允许显示实际的代码)



我已经尝试初始化服务,但在Ember Qunit测试,控制器没有注入到他们的服务。



我已经尝试将注入:init()而不是beforeEach,不工作...



在单元测试中如何注入?



我将调试器中的断点看看我的控制器是否有服务,它不会在测试期间。但是,在正常的Ember服务器上可以正常使用。

解决方案

您不必输入服务。您必须在下面的需求中包含服务。

  moduleFor(controller:test,{
needs: 'service:alias']
});

例如:



服务/别名.js

  Em.service.extend({
name:'john'
});

controllers / test.js

  Em.Controller.extend({
alias:Em.service.inject(),

test:function(){
alert .get('alias.name');
}
});

tests / unit / controllers / test-test.js

  moduleFor('controller:test',{
needs :['service:store']
});

test('别名别名别名',函数(assert){

var controller = this.subject ();
assert.equal(controller.get('store.name'),'john);

});
pre>

为了运行此测试,Ember将生成一个容器,其中包含控制器测试服务别名,所以您可以访问名称为前缀的服务属性。


Specs:

  • Ember version: 1.13.8
  • node: 0.10.33
  • npm: 2.13.4

I have

import Alias from "../../../services/alias";
....

moduleFor("controller:test", "Controller: test", {
  integration: true,

  beforeEach: function() {
    this.register('service:alias', Alias, {singleton: true});
    this.inject.service('alias', { as: 'alias' });
    this.advanceReadiness();
  },
});
...

test('Alias Alias Alias ', function(assert) {
  var controller = this.subject();

  //sample function
  controller.send("test");
  assert.equal(true, controller.alias.get("alias"), "alias should be true");
});

(Using 'alias' as example because I'm not allow to show actual code)

I've tried to initialize the service but during Ember Qunit tests, controllers do not have the services injected to them.

I've tried putting the injection in: init() instead of beforeEach, doesn't work either...

How do I inject it during unit tests?

I put break points in the debugger to see if my controllers have the service, it doesn't during tests. It's fine on normal ember serve, however.

解决方案

You don't have to import service. You have to include service in needs like below.

moduleFor("controller:test", {
   needs: ['service:alias']
});

For eg:

service / alias.js

Em.service.extend({
  name: 'john'
});

controllers / test.js

Em.Controller.extend({
  alias: Em.service.inject(),

  test: function() {
    alert(this.get('alias.name');
    }
  });

tests/unit/controllers/test-test.js

moduleFor('controller:test', {
  needs: ['service:store']
});

test('Alias Alias Alias', function(assert) {

    var controller = this.subject();
    assert.equal(controller.get('store.name'), 'john);

    });

For this test to run, Ember will generate a container with the controller test and service alias. So you can access the service properties with its name prefixed.

这篇关于用于单元测试的EmberJS服务注入(Ember QUnit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆