Ember.js: [BUG/REGRESSION] Injecting autogenerated controllers doesn't work in acceptance tests

Created on 3 May 2017  路  14Comments  路  Source: emberjs/ember.js

REPRODUCTION: https://github.com/cibernox/demo-inject-problem

  1. Clone and yarn
  2. Run the tests
  3. Expected error:
    > Attempting to inject an unknown injection: 'controller:application'

Apparently this only happens in tests. During normal execution it works. It also works if instead of injecting an autogenerated controller you create an empty application controller.

It works in 2.13 but fails in 2.14-beta and canary.

/cc @rwjblue As discussed in slack ^

Has Reproduction Inactive Needs Submitter Response

All 14 comments

Generally speaking here, the error does seem correct. I think controller:application may be the only case where folks would want this, and its general needed for router information (e.g. currentRouteName, currentPath, etc).

Its a tad annoying for the controller:application case because we always generate one. I think the simplest solution here would be to just register controller:application so it is present...

I believe that something changed in 2.13 in the way injection works so now they are eager in acceptance tests. I've found another error in a test, although this time a legit mistake, in which I was importing a service that doesn't exist anymore from a model.

In 2.12, this would pass the tests because I never try to access it. In 2.13 it failed even in tests that don't exercise the functionality.

During regular execution (not in tests) things seem to work the same.

Yes, agreed, 2.13 enables the "no double extend" feature and corrects a number of missing validation scenarios.

I guess what I'm saying is that the validations that are happening (and errors being thrown) are actually correct. However, in the specific case of controller:application, we should just make sure it is present (so the validation error isn't triggered) since we guarantee that it will be created and available (it just isn't' done until after routing is booted now so it fails the assertion depending on timing) .

@rwjblue Got it. Where is the right place to "warm up" the application route? I can do it.

I think you would add something like the following to here:

  registry.register('controller:application', Controller);

Then you would also have to update this test.

This trolled me whilst working on ember-concurrency dummy docs site: https://github.com/machty/ember-concurrency/blob/master/tests/dummy/app/docs/controller.js#L6

No strong opinions as to whether this is breaking enough to be fixed/reverted, just sharing.

Any work arounds for this? I'm getting this issue with 2.13.0. Code looks like this:

_app/controllers/widget/instances.js_

import Ember from 'ember';
const { computed } = Ember;

export default Ember.Controller.extend({
  applicationController: Ember.inject.controller('application'),
  widgetController: Ember.inject.controller('widget'),

  shop: computed.readOnly('applicationController.model'),
  widget: computed.readOnly('widgetController.model'),
  instance: computed.alias('model'),
  configurationSlug: computed.readOnly('widgetController.configure')
});

_tests/unit/controllers/widget/instances-test.js_

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

moduleFor('controller:widget/instances',
  'Unit | Controller | widget/instances', {
    // Specify the other units that are required for this test.
    // needs: ['controller:foo']
  });

test('it exists', function (assert) {
  const controller = this.subject();
  assert.ok(controller);
});

Error:

Assertion Failed: Attempting to inject an unknown injection: 'controller:application'

@djones ember generate controller application should work around the issue.

@locks thanks for the response but that did not fix it. Same error.

Even if I comment out:

applicationController: Ember.inject.controller('application'),

I get:

Assertion Failed: Attempting to inject an unknown injection: 'controller:widget'

That indicates it's not application controller specific to me. It all seems to work fine in browser, just not in tests.

I am getting this exact error outside of tests inside the application when trying to inject the (autogenerated) application-controller in a nested sub-controller.

Got bitten by this in my app today. It happened outside of the test environment. The workaround of generating a concrete controller worked but it feels like controller injection should work with autogenerated controllers.

@cibernox @d-schloss-mvg @djones @kjf @locks @machty @rwjblue is this still an issue, perhaps we should close or create a new reproduction of this, what do you think?

Closing for now, feel free to re-open if you can reproduce in the current release of Ember.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MelSumner picture MelSumner  路  33Comments

rlivsey picture rlivsey  路  34Comments

ctataryn picture ctataryn  路  33Comments

chancancode picture chancancode  路  63Comments

marcoow picture marcoow  路  59Comments