0

我正在尝试使用 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 应用程序测试。

4

1 回答 1

0

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

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

更新

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

另请查看此博客文章以获取有关引入此更改的更新的更多信息。

希望能帮助到你

于 2013-05-17T17:48:50.667 回答