测试Ember.JS应用程序失败,返回ReferenceError:未定义Ember

Testing Ember.JS Application fails with ReferenceError: Ember is not defined

本文关键字:Ember ReferenceError 未定义 返回 失败 JS 应用程序 测试      更新时间:2023-09-26

通过出色的yeoman generator-ember(版本0.7.1)生成ember.js项目后,我尝试使用集成的mocha执行测试。

grunt test

npm test

标准测试运行良好,但没有参考Ember项目。所以自己的模型测试抛出

ReferenceError: Ember is not defined
  at Context.<anonymous> (/home/lray/workspace/js/mediator/test/spec/source_model_test.js:9:9)
  at Hook.Runnable.run (/home/lray/workspace/js/mediator/node_modules/mocha/lib/runnable.js:213:32)
  at next (/home/lray/workspace/js/mediator/node_modules/mocha/lib/runner.js:243:10)
  at Object._onImmediate (/home/lray/workspace/js/mediator/node_modules/mocha/lib/runner.js:254:5)
  at processImmediate [as _immediateCallback] (timers.js:330:15)

这就是上面提到的测试。。。

'use strict';
(function () {
describe('Mediator.Source (Model)', function () {
  beforeEach(function() {
    Ember.run(function () { Mediator.reset(); });
    Ember.testing = true;
  });
  afterEach(function () {
      Ember.testing = false;
  });
  describe('initialize like expected', function () {
      it('should return the given parameters correctly', function(){
        var oItem;
        Ember.run(function () {
          // Won't actually load until the end of the run-block.
          oItem = Mediator.Source.find(1);
        });
        expect(oItem.get("id")).to.be.equal("myId");
        expect(oItem.get("name")).to.be.equal("myName");
        expect(oItem.additional).to.be.false();
      })
  })
});
})();

我的package.json看起来很原封不动:

{
  "name": "mediator",
  "version": "0.0.1",
  "dependencies": {  },
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-copy": "~0.4.1",
    "grunt-contrib-concat": "~0.3.0",
    "grunt-contrib-coffee": "~0.7.0",
    "grunt-contrib-uglify": "~0.2.0",
    "grunt-contrib-compass": "~0.5.0",
    "grunt-contrib-jshint": "~0.6.3",
    "grunt-contrib-cssmin": "~0.6.0",
    "grunt-contrib-connect": "~0.3.0",
    "grunt-contrib-clean": "~0.5.0",
    "grunt-contrib-htmlmin": "~0.1.3",
    "grunt-contrib-imagemin": "0.1.4",
    "grunt-contrib-watch": "~0.5.2",
    "grunt-rev": "~0.1.0",
    "grunt-usemin": "~0.1.12",
    "grunt-mocha": "~0.4.1",
    "grunt-open": "~0.2.0",
    "grunt-svgmin": "~0.2.0",
    "grunt-concurrent": "~0.3.0",
    "load-grunt-tasks": "~0.1.0",
    "connect-livereload": "~0.2.0",
    "grunt-ember-templates": "0.4.14",
    "time-grunt": "~0.1.1",
    "grunt-neuter": "~0.5.0",
    "mocha": "~1.9.0",
    "expect.js": "~0.2.0"
},
"scripts": {
    "test": "mocha --recursive test/spec/*.js"
},
  "engines": {
    "node": ">=0.8.0"
  }
}

更新:require("ember");添加到测试用例文件时,npm test会抱怨

> mediator@0.0.1 test /home/lray/workspace/js/mediator
> mocha --recursive test/spec/*.js
module.js:340
    throw err;
      ^
Error: Cannot find module 'ember'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/lray/workspace/js/mediator/test/spec/source_model_test.js:4:1)

而CCD_ 6只是愉快地忽略了测试文件。

我是否必须以不同的方式将与Ember的联系结合起来?最好的方法是什么?提前感谢。。。

命令行中使用的Mocha是Node.js应用程序。

一般来说,在Node.js中,如果你想使用一些东西,你必须先安装它:

npm install ember

这将在本地node_modules目录中安装Ember。

然后您必须使用对require的调用。在使用mocha进行测试的情况下,在解析这些文件之前,像describeit这样的调用由mocha本身添加到可用于测试文件的符号中。这是一种特殊情况,但其他一切都必须以某种方式进行。

Ember的Node.js文档说要做:

require("ember");

并且CCD_ 11将被添加到您的全局命名空间中。

通过更新yeoman的成员生成器0.8.0。经过项目的重建,这个问题消失了。