Search code examples
ember.jsember-cli

How to access needs elements in ember-cli unit test


In a route test I need to access a service, to retrieve a model. I have referenced the service via needs, but inside the test I don't see a way of accessing the service.

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');

    ...


});

How can I access the service inside the test?

(BTW: I'm using ember v2.0.0)


Solution

  • Found the solution. The key is that when using needs, the resources can be accessed via the container from inside a test:

    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');
     ...
    })