在通过伟大的yeoman generator-ember (Version0.7.1)生成一个generator-ember项目之后,我尝试使用集成的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吗?如何才能做到这一点呢?提前谢谢..。
发布于 2014-01-11 13:28:48
通过更新约曼的余烬-发电机0.8.0。和重建项目,这个问题就消失了。
发布于 2013-12-12 11:24:23
命令行中使用的Mocha是一个Node.js应用程序。
通常,在Node.js中,如果您想要使用某些东西,就必须首先安装它:
npm install ember这将在本地node_modules目录中安装Ember。
然后,您必须使用调用require。在用mocha进行测试的情况下,像describe和it这样的调用是由mocha本身添加到可用于测试文件的符号中,然后再对这些文件进行解析。这是一个特殊的情况,但其他的一切都必须在某种程度上被要求。
安伯尔的Node.js 文档说:
require("ember");Ember将添加到您的全局命名空间中。
https://stackoverflow.com/questions/20541413
复制相似问题