Ember.js: the best way to get a controller in an integration test

I have some integration tests that usually look like this:

test('can load with available teams', function () {
    visit('/workforce/admin/organizations/create').then(function () {
        var controller = App.__container__.lookup('controller:organizations.create');
        ...
    });
});

The underscores in the App .__ container indicate (at least for me) that this is a private property and should not be accessed from outside.

Is there a better way / template to achieve this?

+4
source share
2 answers

During testing, this is the best way to do this, you can create a test assistant to avoid the need to fix it in several places if it changes in the future.

// register custom helper
Ember.Test.registerHelper('getController',  
  function(app, controllerName) {
    return app.__container__.lookup('controller:' + controllerName);
  }
);


test('dblClick link increments count', function() {
  expect(2);
  visit('/').then(function(){
    var c = getController('index');
    ok(c.get('good'));
    ok(!c.get('bad'));
  });
});

http://jsbin.com/jesuyeri/14/edit

+7
source

You can also use ES6 when using @ Kingpin2k answer:

Ember.Test.registerHelper('getController',
  (app, controllerName) => app.__container__.lookup('controller:' + controllerName)
);
+1
source

Source: https://habr.com/ru/post/1543179/


All Articles