【问题标题】:Qunit testing an ember controller, located in a file that contains multiple controllers?Qunit 测试 ember 控制器,位于包含多个控制器的文件中?
【发布时间】:2014-07-25 09:09:58
【问题描述】:

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

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

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

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

在无尽的在线挖掘之后,我found out(如果我错了请纠正我)解析器只关心文件名,而不关心'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`

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

【问题讨论】:

    标签: unit-testing testing ember.js qunit ember-qunit


    【解决方案1】:

    你应该可以用小驼峰式来定义它。

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

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

    【讨论】:

    • 感谢您的回复,但是这对我不起作用,已经尝试过了。问题是 app/controllers/customers_controller.coffee 包含其中定义的 2 个控制器:CustomersIndexController 和 CustomerItemController,我无法测试它们,当我尝试测试 moduleFor("controller:customerItemController", App.CustomerItemController') 时我的 app/tests/controllers/customers-controller-tests 错误
    【解决方案2】:

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

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

    • app/controllers/customers/index.coffee
    • app/controllers/customers/item.coffee

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

    【讨论】:

      猜你喜欢
      • 2014-07-02
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多