通过添加安装Ember助手然后在组件中使用它会破坏测试

时间:2015-04-06 16:28:50

标签: ember.js ember-cli

我正在安装' ember-moment'帮助,然后我在组件中使用它。一旦我这样做,那些组件'测试打破说什么都没有:

not ok 120 PhantomJS 1.9 - StatusCardComponent: it renders
---
    actual: >
        null
    message: >
        Died on test #2     at http://localhost:7357/assets/test-support.js:2779
            at test (http://localhost:7357/assets/test-support.js:1796)
            at http://localhost:7357/assets/client.js:11190
            at http://localhost:7357/assets/vendor.js:150
            at tryFinally (http://localhost:7357/assets/vendor.js:30)
            at http://localhost:7357/assets/vendor.js:156
            at http://localhost:7357/assets/test-loader.js:29
            at http://localhost:7357/assets/test-loader.js:21
            at http://localhost:7357/assets/test-loader.js:40
            at http://localhost:7357/assets/test-support.js:5545: Assertion Failed: A helper named 'moment' could not be found
    Log: >
...

以下是测试本身的代码,它只是自动生成:

import {
  moduleForComponent,
  test
} from 'ember-qunit';

moduleForComponent('event-card', {
  // Specify the other units that are required for this test
  // needs: ['component:foo', 'helper:bar']
});

test('it renders', function(assert) {
  assert.expect(2);

  // Creates the component instance
  var component = this.subject();
  assert.equal(component._state, 'preRender');

  // Renders the component to the page
  this.render();
  assert.equal(component._state, 'inDOM');
});

我尝试了一些方法,将这个帮助程序作为依赖项添加到测试中,如needs: ['helper:moment'],并将其初始化程序签名添加到moduleForComponent函数的参数中,但没有任何效果,我无法做到找到有关它的任何信息。非常感谢帮助。

3 个答案:

答案 0 :(得分:2)

修改:根据此PR,您应该能够指定{ integration: true }将现有的单元测试转换为集成测试,从而无需needs: 1}}。

 moduleForComponent('post', { integration: true }); 

可在此处找到更多背景信息:https://github.com/rwjblue/ember-qunit/issues/108。从本质上讲,集成标志会禁用测试的隔离容器,以便它可以访问应用程序的解析器。

注意:需要ember-qunit@0.2.11.

旧答案:看一下这个PR:https://github.com/switchfly/ember-test-helpers/pull/21,它会添加moduleForIntegration。这是相关位:

  

这增加了一种新的测试模块,填补了当前的空白   一组可能的测试。到目前为止,我们有:

     

•有利于单独测试算法正确性的单元测试。

     

•适用于完整测试的验收测试   应用

     

但我们缺少集成测试小于的单位的能力   整个应用程序。这个PR添加了一个新的TestModuleForIntegration   允许您使用模板驱动集成测试。我想是的   通常是测试Ember组件而不是单元的更合适的方法   测试,因为有趣的组件往往具有丰富的依赖性   其他组件。

您可以在liquid-fire tests中看到它正在使用:

/* global sinon */
import Ember from "ember";
import moduleForIntegration from "../../helpers/module-for-integration";
import { test } from "ember-qunit";

moduleForIntegration('Integration: liquid-if');

test('it should render', function(assert) {
  this.set('person', 'Tom');
  this.render(`
    {{#liquid-if isReady}}
      {{person}} is ready
    {{else}}
      {{person}} is not ready
    {{/liquid-if}}
  `); // }}`)

  assert.equal(this.$().text().trim(), 'Tom is not ready');
  this.set('person', 'Yehuda');
  assert.equal(this.$().text().trim(), 'Yehuda is not ready');
  this.set('isReady', true);
  assert.equal(this.$().text().trim(), 'Yehuda is ready');
});

答案 1 :(得分:2)

所以这就是最终的工作:

import {
    moduleForComponent,
    test
} from 'ember-qunit';

import Ember from 'ember';

import { initialize } from '../../../../../initializers/ember-moment';

moduleForComponent('event-card', {
    // Specify the other units that are required for this test
    // needs: ['helper:moment'],
    setup: function (container) {
        Ember.run(function () {
            // these two arguments are not used
            // but probably still good to pass them in
            // in the event we leverage them in the future
            initialize(container);
        });
    }
});

test('it renders', function (assert) {
    assert.expect(2);

    // Creates the component instance
    var component = this.subject();
    assert.equal(component._state, 'preRender');

    // Renders the component to the page
    this.render();
    assert.equal(component._state, 'inDOM');
});

答案 2 :(得分:0)

你是否看到注释掉的那行: // needs: ['component:foo', 'helper:bar']

您需要取消注释并将其读取:

needs: ['helper:moment']

如果您的moment助手已正确注册

,那应该会有所帮助