Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 存在时,Ember CLI生成的单元测试失败_Javascript_Unit Testing_Ember.js_Ember Cli_Ember Qunit - Fatal编程技术网

Javascript 存在时,Ember CLI生成的单元测试失败

Javascript 存在时,Ember CLI生成的单元测试失败,javascript,unit-testing,ember.js,ember-cli,ember-qunit,Javascript,Unit Testing,Ember.js,Ember Cli,Ember Qunit,我在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', {

我在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/route,其中有人在表中单击一个订单,然后在表下方显示有关该订单的更多详细信息,例如,该路径现在是/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)
在Object.test测试#1时死亡(http://localhost:4200/assets/test-support.js:1644:11)
在http://localhost:4200/assets/orders-app.js:1464:15
在mod.state(http://localhost:4200/assets/vendor.js:150:29)
终于(http://localhost:4200/assets/vendor.js:30:14)
按要求(http://localhost:4200/assets/vendor.js:148:5)
在Object.TestLoader.require(http://localhost:4200/assets/test-loader.js:29:9)
位于Object.TestLoader.loadModules(http://localhost:4200/assets/test-loader.js:21:18):需要[controller:orders],但找不到它
错误:需要[controller:orders],但找不到它
新错误时(本机)
错误的,错误的(http://localhost:4200/assets/vendor.js:22707:21)
在验证需求独立性时(http://localhost:4200/assets/vendor.js:13783:13)
在ControllerMixin.default.reopen.init处(http://localhost:4200/assets/vendor.js:13865:11)
在超级包装器上[作为初始](http://localhost:4200/assets/vendor.js:27980:20)
在新课上(http://localhost:4200/assets/vendor.js:41203:14)
在Function.ClassMixinProps.create处(http://localhost:4200/assets/vendor.js:41625:14)
在exports.default.klassy.Klass.extend.defaultSubject处(http://localhost:4200/assets/test-support.js:2188:22)
位于Object.exports.default.klassy.Klass.extend.contextualizeCallbacks.context.(匿名函数)[作为主题](http://localhost:4200/assets/test-support.js:2209:41)
反对。(http://localhost:4200/assets/orders-app.js:1465:27)

要使单元测试通过,我需要在设置或测试中更改什么?

您需要通知测试控制器依赖关系。为此,请将moduleFor方法更改为以下内容:

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

这导致错误更改为:beforeach failed on exists:尝试注册未知工厂:
controller:orders
是否有名为orders.js的控制器?如果没有,您可以从order.js和order-test.js中删除“needs”指令。这就解决了问题,添加订单控制器也解决了问题。