Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js: Best way to get controller in integration test

Tags:

ember.js

I have some integration tests which 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 App.__container indicates (for me at least), that this is a private property and should not be accessed externally.

Is there a better way/pattern to achieve this?

like image 788
AyKarsi Avatar asked Jun 04 '14 08:06

AyKarsi


2 Answers

During testing this is the best way to do it, you can create a test helper so as to avoid having to fix it in multiple places in the event that 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

like image 189
Kingpin2k Avatar answered Sep 28 '22 15:09

Kingpin2k


You can also take advantage of ES6 when using @Kingpin2k's answer:

Ember.Test.registerHelper('getController',
  (app, controllerName) => app.__container__.lookup('controller:' + controllerName)
);
like image 22
Daniel Kmak Avatar answered Sep 28 '22 15:09

Daniel Kmak