【问题标题】:Rendered component test fails after Ember 1.10 upgradeEmber 1.10 升级后渲染组件测试失败
【发布时间】:2015-05-16 06:51:05
【问题描述】:

我正在对组件进行单元测试,尤其是呈现的表单。我正在接近这个as described in the 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')

(当我输入console.log 检查该比较时,我确实得到5 > 25 > 2。)组件?

这是 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 更新。

预计到达时间:请注意下面 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() 未定义。

【问题讨论】:

  • 我刚刚将一些问题发布到了 ember-mocha repo github.com/switchfly/ember-mocha/issues/25,正如您所看到的 @rwjblue 的回复,文档不正确。我只是没有时间更新它们。
  • 这就解释了为什么andThen 不起作用,谢谢。仍然不确定为什么没有它们测试会失败,尽管现在听起来严格遵循指南示例并不是构建测试的最可靠方法。

标签: ember.js ember-qunit


【解决方案1】:

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

  • 更多组件更新,我想只是因为距离我第一次尝试已经过去了一段时间。 ember-resolverloader.jsember-cli-app-versionember-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() 块。

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 2015-04-12
    相关资源
    最近更新 更多