如何在Ember集成测试中设置ember-intl服务?

时间:2018-05-29 12:09:58

标签: ember.js integration-testing

我刚开始将ember-intl添加到我正在进行测试的应用程序中。我的验收测试仍然有效,但我对其模板使用ember-intl进行字符串转换的组件的集成测试失败了:

“未定义区域设置。无法解析翻译:...”

ember-intl docs there is a section on Integration Testing,似乎已过时:

import hbs from 'htmlbars-inline-precompile';
import wait from 'ember-test-helpers/wait';
import { moduleForComponent, test } from 'ember-qunit';

let service;

moduleForComponent('x-product', 'XProductComponent', {
  integration: true,
  setup() {
    service = this.container.lookup('service:intl');
    service.setLocale('en-us');
  }
});

test('it renders', function(assert) {
  assert.expect(1);
  this.render(hbs`{{x-product price=price deadline=deadline}}`);
  this.set('price', 1000);
  this.set('deadline', new Date());
  let output = this.$().text();
  assert.ok(output);
});

test('it translates', function(assert) {
  assert.expect(1);

  /* waits for async behavior (loading translations on app boot) to settle */
  return wait().then(() => {
    assert.equal(service.t('some.key'), 'Hello world');
  });
});

我查看了Ember文档,我可以看到how to stub a service for testing,但不知道如何在测试中加载服务然后使用它。

1 个答案:

答案 0 :(得分:2)

我们现在需要在新的格式测试中使用this.container,而不是使用this.owner。这是一段代码,展示了如何在上下文中使用它:

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { find, render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';


module('Integration | Component | login-form', function(hooks) {
  setupRenderingTest(hooks);

  let service;

  hooks.beforeEach(function() {
     service = this.owner.lookup('service:intl');
     service.setLocale('en-us');
  });

  test('it renders', async function(assert) {
    await render(hbs`{{login-form}}`);

    assert.equal(find('[data-test-login-title]').textContent.trim(), 'Login');
  });
});

PR已提交给ember-intl,因此希望文档能够尽快反映最新的最佳做法。