Ember CLI generated unit test is failing on it exists

791 views Asked by At

I have an order controller in controllers/order.js

import Ember from 'ember';

export default Ember.Controller.extend({
  needs: "orders"
});

This has a test in /tests/unit/controllers/order-test.js

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

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

// Replace this with your real tests.
test('it exists', function(assert) {
var controller = this.subject();
assert.ok(controller);
});

router.js looks like this

this.resource('orders', function() {
  this.resource('order', { path: ":order_id"} , function() {
  });
  this.route('create');
});

The idea being I have a /orders/ route where someone clicks an order in a table and then below the table it shows further details on the order and the route is now /orders/1 for example.

The unit tests fails with:

Died on test #1     at Object.test (http://localhost:4200/assets/test-support.js:1644:11)
at http://localhost:4200/assets/orders-app.js:1464:15
at mod.state (http://localhost:4200/assets/vendor.js:150:29)
at tryFinally (http://localhost:4200/assets/vendor.js:30:14)
at requireModule (http://localhost:4200/assets/vendor.js:148:5)
at Object.TestLoader.require (http://localhost:4200/assets/test-loader.js:29:9)
at Object.TestLoader.loadModules (http://localhost:4200/assets/test-loader.js:21:18): <(subclass of Ember.Controller):ember216> needs [ controller:orders ] but it could not be found

Error: <(subclass of Ember.Controller):ember216> needs [ controller:orders ] but it could not be found
at new Error (native)
at Error.EmberError (http://localhost:4200/assets/vendor.js:22707:21)
at verifyNeedsDependencies (http://localhost:4200/assets/vendor.js:13783:13)
at ControllerMixin.default.reopen.init (http://localhost:4200/assets/vendor.js:13865:11)
at superWrapper [as init] (http://localhost:4200/assets/vendor.js:27980:20)
at new Class (http://localhost:4200/assets/vendor.js:41203:14)
at Function.ClassMixinProps.create (http://localhost:4200/assets/vendor.js:41625:14)
at exports.default.klassy.Klass.extend.defaultSubject (http://localhost:4200/assets/test-support.js:2188:22)
at Object.exports.default.klassy.Klass.extend.contextualizeCallbacks.context.(anonymous function) [as subject] (http://localhost:4200/assets/test-support.js:2209:41)
at Object.<anonymous> (http://localhost:4200/assets/orders-app.js:1465:27)

What do I need to change in my setup or test to get the unit test passing here?

1

There are 1 answers

3
Diné Bennett On BEST ANSWER

You need to inform the test of the controller dependencies. In order to do this, change your moduleFor method to the following:

moduleFor('controller:order', {
  needs: ['controller:orders']});