如何设置mocha测试以运行Ember初始化程序,以便定义注入的对象?

时间:2022-07-07 01:12:18

My Ember app injects an "i18n" object into the container via an initializer, which is later looked up with "this.container.lookup('i18n:main')" in a controller 'preferredLanguage' computed property.

我的Ember应用程序通过初始化程序将“i18n”对象注入容器,稍后在控制器'preferredLanguage'计算属性中查找“this.container.lookup('i18n:main')”。

A mocha unit test which tests the controller 'preferredLanguage' property, fails due to "i18n is not defined". How can I set up the mocha tests to run Ember application initializers so injected objects are defined when looked up from the container during unit testing?

测试控制器'preferredLanguage'属性的mocha单元测试由于“i18n未定义”而失败。如何设置mocha测试以运行Ember应用程序初始化程序,以便在单元测试期间从容器中查找时定义注入的对象?

1 个解决方案

#1


I've found that the main issue is (as you mentioned) is that the start-app.js file doesn't run when mocha is installed. I battled this for a while as well but have finally refined the process to get Ember and Mocha to play nicely. First you have to get ember-cli-mocha and ember-mocha setup correctly. Then you can explicitly import and invoke the startApp function in your tests/test-helper.js file to have Ember run and inject test helpers like it normally does with qunit. Here is what has worked for me with ember-cli 1.13.1.

我发现主要的问题是(正如你所提到的)安装mocha时start-app.js文件没有运行。我和他争斗了一段时间,但终于改进了让Ember和Mocha玩得很好的过程。首先,你必须正确设置ember-cli-mocha和ember-mocha。然后你可以在tests / test-helper.js文件中显式导入和调用startApp函数,让Ember运行并注入测试助手,就像它通常使用qunit一样。以下是ember-cli 1.13.1对我有用的内容。

bower install ember-mocha
bower install ember-test-helpers
npm install ember-cli-mocha
ember install ember-cli-mocha (say Y to overwrite test-helper.js)

Then in tests/test-helper.js

然后在tests / test-helper.js中

// tests/test-helper.js
import resolver from './helpers/resolver';
import { setResolver } from 'ember-mocha';

// startApp doesn't run with mocha... so we run it explicitly
import startApp from "./helpers/start-app";
startApp();

setResolver(resolver);

After that you can create generate a route or controller and ember-cli-mocha will create test and you should have access to helpers like visit() and currentURL(); Though I found you need to actually setup the route and controller for those to work correctly.

之后你可以创建生成路由或控制器,ember-cli-mocha将创建测试,你应该可以访问helser,如visit()和currentURL();虽然我发现你需要实际设置路由和控制器才能正常工作。

it("should have use of ember's test helpers", function() {
  visit("/mocha-test");
  andThen(function() {
    var url = currentURL();
    expect(url).to.equal("/mocha-test");
  });
});

#1


I've found that the main issue is (as you mentioned) is that the start-app.js file doesn't run when mocha is installed. I battled this for a while as well but have finally refined the process to get Ember and Mocha to play nicely. First you have to get ember-cli-mocha and ember-mocha setup correctly. Then you can explicitly import and invoke the startApp function in your tests/test-helper.js file to have Ember run and inject test helpers like it normally does with qunit. Here is what has worked for me with ember-cli 1.13.1.

我发现主要的问题是(正如你所提到的)安装mocha时start-app.js文件没有运行。我和他争斗了一段时间,但终于改进了让Ember和Mocha玩得很好的过程。首先,你必须正确设置ember-cli-mocha和ember-mocha。然后你可以在tests / test-helper.js文件中显式导入和调用startApp函数,让Ember运行并注入测试助手,就像它通常使用qunit一样。以下是ember-cli 1.13.1对我有用的内容。

bower install ember-mocha
bower install ember-test-helpers
npm install ember-cli-mocha
ember install ember-cli-mocha (say Y to overwrite test-helper.js)

Then in tests/test-helper.js

然后在tests / test-helper.js中

// tests/test-helper.js
import resolver from './helpers/resolver';
import { setResolver } from 'ember-mocha';

// startApp doesn't run with mocha... so we run it explicitly
import startApp from "./helpers/start-app";
startApp();

setResolver(resolver);

After that you can create generate a route or controller and ember-cli-mocha will create test and you should have access to helpers like visit() and currentURL(); Though I found you need to actually setup the route and controller for those to work correctly.

之后你可以创建生成路由或控制器,ember-cli-mocha将创建测试,你应该可以访问helser,如visit()和currentURL();虽然我发现你需要实际设置路由和控制器才能正常工作。

it("should have use of ember's test helpers", function() {
  visit("/mocha-test");
  andThen(function() {
    var url = currentURL();
    expect(url).to.equal("/mocha-test");
  });
});