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

AyK*_*rsi 2 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');

    ...


});
Run Code Online (Sandbox Code Playgroud)

如何在测试中访问服务?

(顺便说一句:我正在使用ember v2.0.0)

AyK*_*rsi 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');
 ...
})
Run Code Online (Sandbox Code Playgroud)