我正在对组件进行单元测试,尤其是渲染的表单。我正在接近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 > 2
console.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()
未定义。