1

所以,我一直在尝试对 Ember 控制器进行 qunit 测试,问题是,控制器位于一个包含多个控制器的咖啡脚本文件中。

现在,ember 测试指南说,为了测试控制器,我应该像这样使用“moduleFor”助手:

moduleFor(fullName [, description [, callbacks]])

在我的例子中,全名是:“CustomersIndexController”,但是因为它包含在“customers_controller.coffee”中,它本身包含多个控制器,所以测试它变得有问题。

在无尽的在线挖掘之后,我发现(如果我错了,请纠正我)解析器只关心文件名,而不关心'export default myModel'提供的名称

为了更清楚,这是我的“customers_controller.coffee”:

`export { CustomersIndexController, CustomersItemController }`

CustomersIndexController = Ember.ArrayController.extend
#Code goes here ......

CustomerItemController = Ember.ObjectController.extend
#Code goes here .....

这是customers-controller-test.coffee文件:

`import { test, moduleFor } from 'ember-qunit';`
 moduleFor("controller:customers-index-controller", 'C Controller') 

 test "it's an App.Controller", -> ok(@subject())

我已经尝试了我的大脑可以产生的所有想法......没有任何运气(将控制器名称从 camelCase 更改为 dasherized,更改为绝对路径,甚至尝试导入 customers_controller.coffee),但我不断得到:

Setup failed on it's a App.Controller: Attempting to register an unknown factory: `controller:customers-index-controller`

任何帮助/建议/链接都非常感谢。

4

2 回答 2

2

您应该能够在较低的驼峰式中定义它。

moduleFor('controller:postsIndex', 'Posts Index Controller');

http://jsbin.com/ruhalota/1/edit

于 2014-06-05T03:06:33.823 回答
0

如果您使用 ember-cli 查看解析器的文档,您会发现它确实只关心文件的名称,以及它们的默认导出是什么:http://www.ember- cli.com/#using-modules

在您的情况下,您需要将控制器拆分为多个文件,以便解析器可以正确找到并实例化它们。因此,这两个文件将是:

  • 应用程序/控制器/客户/index.coffee
  • 应用程序/控制器/客户/item.coffee

这一切都假设您使用的是 ember-cli。如果您仍在使用 ember-app-kit,您可能需要稍微调整一下,但应该适用相同的基本思想。

于 2015-01-05T13:23:28.300 回答