Ember CLI 生成的单元测试因它存在而失败
Ember CLI generated unit test is failing on it exists
我在 controllers/order.js
中有一个订单控制器
import Ember from 'ember';
export default Ember.Controller.extend({
needs: "orders"
});
这在/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 看起来像这样
this.resource('orders', function() {
this.resource('order', { path: ":order_id"} , function() {
});
this.route('create');
});
我的想法是我有一条 /orders/ 路线,有人在 table 中点击一个订单,然后在 table 下方显示订单的更多详细信息,路线现在是 /orders/1 例如。
单元测试失败:
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)
我需要在设置或测试中更改什么才能使单元测试通过此处?
您需要通知控制器依赖项的测试。为此,请将您的 moduleFor 方法更改为以下内容:
moduleFor('controller:order', {
needs: ['controller:orders']});
我在 controllers/order.js
中有一个订单控制器import Ember from 'ember';
export default Ember.Controller.extend({
needs: "orders"
});
这在/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 看起来像这样
this.resource('orders', function() {
this.resource('order', { path: ":order_id"} , function() {
});
this.route('create');
});
我的想法是我有一条 /orders/ 路线,有人在 table 中点击一个订单,然后在 table 下方显示订单的更多详细信息,路线现在是 /orders/1 例如。
单元测试失败:
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)
我需要在设置或测试中更改什么才能使单元测试通过此处?
您需要通知控制器依赖项的测试。为此,请将您的 moduleFor 方法更改为以下内容:
moduleFor('controller:order', {
needs: ['controller:orders']});