【问题标题】:Testing Ember.JS Application fails with ReferenceError: Ember is not defined测试 Ember.JS 应用程序失败并出现 ReferenceError:未定义 Ember
【发布时间】:2013-12-12 10:49:50
【问题描述】:

通过伟大的 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)

grunt test 很高兴地忽略了测试文件。

我是否必须以不同的方式将与 Ember 的连接组合在一起?最好的方法是什么?在此先感谢...

【问题讨论】:

    标签: javascript ember.js mocha.js yeoman


    【解决方案1】:

    从命令行使用的 Mocha 是一个 Node.js 应用程序。

    通常,在 Node.js 中,如果你想使用某些东西,你必须先安装它:

    npm install ember
    

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

    那么你必须调用require。在使用 mocha 进行测试的情况下,在解析这些文件之前,mocha 本身会将诸如 describeit 之类的调用添加到可用于测试文件的符号中。这是一种特殊情况,但其他一切都必须以某种方式要求。

    Ember 的 Node.js doc 说要做:

    require("ember");
    

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

    【讨论】:

    • 嗨@Louis,感谢您的超快速响应。我早些时候尝试过,但是当 grunt 开始忽略测试用例时,mocha 抱怨没有找到任何“ember”模块。您对方法有什么不同的想法吗?
    【解决方案2】:

    通过更新yeomanember-generator 0.8.0. 并重建项目,这个问题就消失了。

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多