【发布时间】: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 > 2 的5 > 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