使用Mocha和Chai测试Ember(v1.0.0-rc.3)嵌套控制器

时间:2013-05-17 16:36:16

标签: ember.js mocha chai

我正在尝试使用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

现在,每当我尝试运行testcase时,我都会遇到错误

    "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应用测试,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

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

<强>更新

在阅读您的评论后,我认为您的问题是,当Ember.testing = true等同于之前需要的App.deferReadiness()时,显然有必要在使用之前'初始化'您的应用,这是通过全局App.initialize()挂钩中的before完成的。最后,您在App.reset()挂钩内拨打beforeEach

请查看此blog post以获取有关引入此更改的更新的详细信息。

希望有所帮助