簡體   English   中英

測試Ember.JS應用程序失敗並出現ReferenceError:未定義Ember

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

通過巨大的yeoman generator-ember -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的連接組合在一起? 最好的方法是怎么做? 提前致謝...

從命令行使用的Mocha是Node.js應用程序。

通常,在Node.js中,如果要使用某些組件,則必須先安裝它:

npm install ember

這會將Ember安裝在本地的node_modules目錄中。

然后,您必須使用呼叫來require 在使用Mocha進行測試的情況下,在解析這些文件之前,mocha本身it諸如describeit類的調用添加到可用於測試文件的符號中。 這是一個特例,但其他所有內容都必須以某種方式進行要求。

Ember的Node.js 文檔說:

require("ember");

Ember將被添加到您的全局名稱空間。

通過更新yeomanember-generator 0.8.0。 然后重建項目,這個問題就解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM