【问题标题】:ember testing component with yield余烬测试组件与产量
【发布时间】:2015-09-12 19:14:45
【问题描述】:

我无法通过 yield 块表达式将编译后的 html 输入到组件中。见测试:

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

moduleForComponent('pro-tab-link', 'Unit | Component | pro tab link', {
  // Specify the other units that are required for this test
  // needs: ['component:foo', 'helper:bar'],
  unit: true
});

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

  // Creates the component instance
  var component = this.subject({
    template: Ember.Handlebars.compile(
      "<h1>Hello world</h1>"
    )
  });

  console.log(component);
  assert.equal(component._state, 'preRender');

  var $component = this.append();

  Ember.run(function() {
   component.set('isOpen', true);
 });

  console.log(component);
  console.log($component.html());

  assert.equal(component._state, 'inDOM');
  assert.ok(component.isOpen);
});

还有模板:

if isOpen
  == yield

由于某种原因,h1 标记没有被传递到组件的产量中。我如何让这个工作?

【问题讨论】:

    标签: ember.js qunit


    【解决方案1】:

    您应该按照fantastic article 关于该主题的内容从对组件进行单元测试切换到为它们编写集成测试。

    您找不到 h1 的原因是这是一个单元测试(即,我们只测试 component.js),我们应该期望在单元测试中无法访问 DOM - 因此切换到集成测试的建议,一个高于单元但低于验收测试的测试水平。

    通过这种方式,您将能够在 DOM 中测试组件及其与模板的交互。

    【讨论】:

    • 但是 ember 的文档不是到处都有这个(在 DOM 中查找元素)吗? guides.emberjs.com/v1.10.0/testing/testing-components
    • 在您发布的链接中,向下滚动到它讨论集成测试助手的部分。有一行将模板附加到 DOM。
    • this.append() 在你这样做之前,没有什么可以运行断言。一旦你这样做了,我们将进行集成测试而不是单元测试。
    • 嗯,但是如果您查看文档中的示例,其中使用了this.append()this.subject() 也被使用,但this.subject() 不是集成测试的一部分。我收到以下错误:Error: component integration tests do not support 'subject()'.
    • 我认为问题的根源仍然存在,由于某种原因,yield 没有获得传递给它的模板。查看这些测试:github.com/huafu/ember.js/blob/…
    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多