【问题标题】:Testing Ember (v1.0.0-rc.3) nested controllers using Mocha and Chai使用 Mocha 和 Chai 测试 Ember (v1.0.0-rc.3) 嵌套控制器
【发布时间】:2013-05-17 16:36:15
【问题描述】:

我正在尝试使用 Mocha 和 Chai 为 Ember (v1.0.0-rc.3) 应用程序的控制器编写测试用例。我的一个控制器正在使用另一个控制器,如下所示

App.ABCController = Em.ObjectController.extend({
  needs: ['application'],

  welcomeMSG: function () {
    return 'Hi, ' + this.get('controllers.application.name');
  }.property(),
  ...
  });

我写的testCase如下:

  describe 'ABCController', ->
  expect = chai.expect
  App = require '../support/setup'
  abcController = null

  before ->
    App.reset()
    ApplicationController = require 'controllers/application_controller'
    ABCController = require 'controllers/abc_controller'
    applicationController = ApplicationController.create()
    abcController = ABCController.create()

  describe '#welcomeMSG', ->
    it 'should return Hi, \'user\'.', ->
      msg = abcController.get('welcomeMSG')
      expect(msg).to.be.equal('Hi, '+ applicationController.get('name'))

support/setup file is as follows
Em.testing = true
App = null
Em.run ->
  App = Em.Application.create()
module.exports = App

现在每当我尝试运行测试用例时都会遇到错误

    "Before all" hook:
TypeError: Cannot call method 'has' of null
    at verifyDependencies (http://localhost:3333/test/scripts/ember.js:27124:20)
    at Ember.ControllerMixin.reopen.init (http://localhost:3333/test/scripts/ember.js:27141:9)
    at superWrapper [as init] (http://localhost:3333/test/scripts/ember.js:1044:16)
    at new Class (http://localhost:3333/test/scripts/ember.js:10632:15)
    at Function.Mixin.create.create (http://localhost:3333/test/scripts/ember.js:10930:12)
    at Function.Ember.ObjectProxy.reopenClass.create (http://localhost:3333/test/scripts/ember.js:11756:24)
    at Function.superWrapper (http://localhost:3333/test/scripts/ember.js:1044:16)
    at Context.eval (test/controllers/abc_controller_test.coffee:14:47)
    at Hook.Runnable.run (test/vendor/scripts/mocha-1.8.2.js:4048:32)
    at next (test/vendor/scripts/mocha-1.8.2.js:4298:10)

请帮我解决这个问题。如果有人提供一些链接,我将不胜感激,我可以在其中学习使用 mocha 和 chai 进行最新的 ember.js 应用程序测试。

【问题讨论】:

  • 这行不应该是expect(msg).to.equal('Hi, '+ applicationController.get('name'))吗?我的意思是使用to.equal 而不是to.be.equal

标签: ember.js mocha.js chai


【解决方案1】:

通过docs 阅读我猜你的问题是你的expect 由于这个to.be.equal 而失败。尝试将断言链更改为:

expect(msg).to.equal('Hi, '+ applicationController.get('name'))

更新

阅读您的评论后,我想您的问题是,当您执行Ember.testing = true 时,就相当于之前需要的App.deferReadiness(),因此显然有必要在使用之前“初始化”您的应用程序,这已完成在您的全局 before 挂钩中使用 App.initialize()。最后,您在 beforeEach 钩子中调用 App.reset()

请查看此blog post,了解有关引入此更改的更新的更多信息。

希望对你有帮助

【讨论】:

  • 谢谢,但这不是我的问题的解决方案,它与 ember 相关。
  • 也许您缺少App.initialize() 而不是App.reset()?因为如果你使用Ember.testing = true,你需要在全局before钩子中直接调用App.initialize()App.reset() 是你在 before each 钩子中调用的内容。
猜你喜欢
  • 2018-05-06
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2013-04-11
  • 2021-01-01
  • 2016-10-05
  • 2018-05-05
  • 2018-02-27
相关资源
最近更新 更多