如何在Ember 3.2测试中加载自定义测试助手?

时间:2018-07-14 17:52:39

标签: ember.js ember-testing

我似乎缺少一些有关如何在Ember 3.2中注册测试助手的信息,需要一些指导:

我正在尝试使用放置在tests/helpers目录中的测试助手(see here),并试图在测试中引用它:

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

module('Integration | Component | my-component', function(hooks) {
  setupRenderingTest(hooks);
  setupMirage(hooks);

  test('it renders nothing by default', async function(assert) {
    const company = server.create('company');
    this.pushMirageRecordsToStore(); // this doesn't work
    await render(hbs`{{my-component}}`);

    assert.equal(this.element.textContent.trim(), '');

  });

调用this.pushMirageRecordsToStore()时,错误是:

TypeError: this.pushMirageRecordsToStore is not a function

这使我相信该链接中的registerAsyncHelper助手没有被调用。

我的问题是如何使Ember测试框架在该测试帮助器中调用registerAsyncHelper,以便可以进行this.pushMirageRecordsToStore()

1 个答案:

答案 0 :(得分:0)

如果您要注册一个测试助手并将其命名为pushMirageRecordsToStore,那么它应该作为全局变量可用,因此您应该可以在测试中以pushMirageRecordsToStore();的身份访问它,而不是this.pushMirageRecordsToStore();

在ember文档here中有关于注册测试助手的良好文档。

p.s。只是为了确认,您在链接中共享的帮助者称为pushMirageDbIntoStore,但此处您称为pushMirageRecordsToStore。名称必须相同:-)

相关问题