1

我正在对组件进行单元测试,尤其是渲染的表单。我正在接近Ember Guides 中的描述

特别是,该组件具有三个计算属性,它们根据支持模型在渲染元素上显示不同的类。我正在调整Ember.run()块中的属性,然后再次查看渲染的组件。

这里有趣的是,即使我触摸了他们观察到的属性,计算的属性似乎也没有重新计算。稍后测试渲染的测试 - 只是组件的返回 - 确实通过了。

这是我的测试代码:

moduleForComponent('wizard-tab', "Component - WizardTab", {
  setup: function () {
    this.tab = this.subject({ step: 2, stepCompleted: 1, tab: tabs.all()[1] });
  }
});

test('#render', function () {
  let tab = this.tab;
  ok(this.$().find('span.wizard-tab-detail').length, "Active tab has a detail span"); // Passes
  // Note that both of the additional states observe stepCompleted
  // so I need to touch that to get them to recalculate
  Ember.run( function () {
    tab.set('stepCompleted', 2);
    tab.set('tab', WizardTab.all()[4]);
  });

  ok(this.$().find('span.wizard-tab-icon-disabled').length, "Future tabs have a disabled class"); // Fails

  Ember.run( function () {
    tab.set('stepCompleted', 3);
    tab.set('tab', WizardTab.all()[1]);
  });

  ok(this.$().find('span.wizard-tab-icon-done').length, "Inactive tabs have a done class"); // Fails
});

第一个断言通过,接下来的两个断言失败。使用console.log我已经验证set()s 正在工作的语句,但是从它们计算的属性返回了错误的结果。

这是计算的属性定义之一:

  disabled: function() {
    return this.get('tab.stepNumber') > (this.get('stepCompleted') + 1);
  }.property('stepCompleted')

(当我检查该比较时,我真的得到false了。)当我检查组件的后续渲染时,我是否缺少一些会阻止更新的东西?5 > 2console.log

这是 ember CLI 0.2.0、Ember 1.10.0 和 ember-cli-qunit 0.3.8。

ETA:可能相关:此测试在 Ember 1.8 和 ember-cli-qunit 0.3.1 上通过。这是 Ember CLI 0.2.0 的更新以及伴随的 Ember 和 ember-cli-qunit 更新导致失败。

ETA:请注意下面的 kiwiupover 评论,下面的这一部分与问题无关;指南可能没有显示当前最好的方法。)

请注意,指南使用类似的模式:

test('changing colors', function() {

  // this.subject() is available because we used moduleForComponent
  var component = this.subject();

  // we wrap this with Ember.run because it is an async function
  Ember.run(function() {
    component.set('name','red');
  });

  // first call to $() renders the component.
  equal(this.$().attr('style'), 'color: red;');

  // another async function, so we need to wrap it with Ember.run
  Ember.run(function() {
    component.set('name', 'green');
  });

  equal(this.$().attr('style'), 'color: green;');
});

我尝试将第二个和第三个断言包含在内,andThen()但引发了错误 -andThen()未定义。

4

1 回答 1

0

我通过启动一个新的开发分支(我们的默认分支)并重新运行更新来完成这项工作。以下是我的原始通行证与有效通行证之间的区别:

  • 更多的组件更新,我想只是因为我第一次尝试已经过去了一段时间。ember-resolver, loader.js,ember-cli-app-version并且ember-cli-dependency-checker都向上移动了。我不知道这些是否重要,但它们确实发生了变化。
  • 我认为,关键部分是将三个测试隔离在单独的测试块中,并在每个测试块中更新主题,Ember.run()这些测试使用来自设置组件的不同属性值。

以下是三个测试通过时的样子:

moduleForComponent('wizard-tab', "Component - WizardTab", {
  setup: function () {
    this.tab = this.subject({ step: 2, stepCompleted: 1, tab: WizardTab.all()[1] });
  }
});

test('Rendered active tabs have a detail span', function () {
  let tab = this.tab;
  ok(this.$().find('span.wizard-tab-detail').length, "Active tab has a detail span");
});

test('Rendered future tabs have a disabled class', function () {
  let tab = this.tab;
  Ember.run( function () {
    tab.set('step', 2);
    tab.set('stepCompleted', 2);
    tab.set('tab', WizardTab.all()[4]);
  });
  ok(this.$().find('span.wizard-tab-icon-disabled').length, "Future tabs have a disabled class");
});

test('Rendered inactive tabs have a done class', function () {
  let tab = this.tab;
  Ember.run( function () {
    tab.set('step', 2);
    tab.set('stepCompleted', 3);
    tab.set('tab', WizardTab.all()[1]);
  });
  ok(this.$().find('span.wizard-tab-icon-done').length, "Inactive tabs have a done class");
});

我相信最后的改变——从一个带有一些Ember.run()块的测试变为三个——是真正做到了。我{{log value}}在模板中使用了一些行来查看哪些值被发送到模板,并且在setup我添加块之前,它一直使用块中的主题三次Ember.run()

于 2015-03-19T15:48:19.913 回答