简体   繁体   中英

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

After generating a ember.js project via the great yeoman generator-ember (version 0.7.1) I try to execute tests with the integrated mocha .

grunt test

or

npm test

The standard test works fine, but it doe not reference the Ember project. So the own model tests throws

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)

This is the mentioned test...

'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();
      })
  })
});
})();

My package.json looks pretty untouched:

{
  "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"
  }
}

Update: When adding require("ember"); to the test case file, npm test complains

> 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)

while grunt test happily just ignores the test file.

Do I have to somehow fit together a connection to Ember differently? How is the best way to do so? Thanks in advance...

Mocha used from the command line is a Node.js application.

Generally, in Node.js if you want to use something you have to install it first:

npm install ember

This will install Ember in a local node_modules directory.

Then you have to use a call to require . In the case of testing with mocha, calls like describe and it are added by mocha itself to the symbols available to test files before these files are parsed. It's a special case, but everything else has to be required in some way.

The Node.js doc for Ember says to do:

require("ember");

and Ember will be added to your global namespace.

Through an update to yeoman 's ember-generator 0.8.0. and a rebuild of the project, this problem was gone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM