2013-12-12 69 views
1

通過偉大的yeoman generator-ember(版本0.7.1)生成ember.js項目後,我嘗試使用集成的mocha執行測試。通過ReferenceError測試Ember.JS應用程序失敗:未定義Ember

grunt test 

npm test 

標準的測試工作正常,但美國能源部不能引用灰燼項目。所以自己的模型試驗拋出

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抱怨

> [email protected] 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) 

while grunt test幸福地忽略了測試文件。

我是否必須以某種方式將連接安裝到Ember上的方式不同?如何做到這一點最好的方式?在此先感謝...

回答

1

從命令行使用的摩卡是一個Node.js應用程序。

一般來說,Node.js的,如果你想使用你有什麼事情先安裝它:

npm install ember 

這將在本地node_modules目錄中安裝灰燼。

然後,您必須使用致電require。在使用摩卡進行測試的情況下,在分析這些文件之前,通過mocha自己將諸如describeit的調用添加到可用於測試文件的符號。這是一個特殊情況,但是其他一切都需要以某種方式進行。

爲灰燼Node.js的doc說做:

require("ember"); 

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

+0

嗨@路易斯,thnx爲超高速響應。我早些時候嘗試過,但當grunt開始時忽略了測試用例,mocha抱怨沒有找到任何'ember'模塊。你有什麼不同的想法嗎? –

相關問題