如何访问ember-cli单元测试中的需求元素

时间:2015-06-19 06:15:07

标签: ember.js ember-cli

在路由测试中,我需要访问服务,以检索模型。 我已根据需要引用了该服务,但在测试中我没有看到访问该服务的方法。

import {
  moduleFor,
  test
} from 'ember-qunit';


moduleFor('route:application', {
  needs: ['route:application','controller:application','service:dialog']
});

test('can open and remove a dialog dialog', function(assert) {
    var route = this.subject();
    route.setProperties({
      controller: { // don't like this part as well .. 
        dialogs:null
      }
    });

    // need to access the service here to get a model
    // something like :
    //var service = this.get('service:dialog');
    var modalModel = service.getModal('business/contract-edit');

    ...


});

如何在测试中访问服务?

(顺便说一句:我使用的是ember v2.0.0)

1 个答案:

答案 0 :(得分:5)

找到解决方案。关键是当使用需求时,可以通过容器从测试内部访问资源:

moduleFor('route:application', {
  // Specify the other units that are required for this test.
  needs: ['route:application','controller:application','service:dialog']
});

test('can open and remove a dialog dialog', function(assert) {
  var route = this.subject();
  var controller = this.container.lookup('controller:application');
  var service = this.container.lookup('service:dialog');
 ...
})