Javascript 流星速度与茉莉花没有返回预期的结果?
我正在尝试测试以下手动工作的功能:Javascript 流星速度与茉莉花没有返回预期的结果?,javascript,meteor,jasmine,velocity,Javascript,Meteor,Jasmine,Velocity,我正在尝试测试以下手动工作的功能: 将用户列表返回为 单击按钮可将的计数减少一 这似乎不起作用: it("should show one less person if you tap you liked them", function() { var personLength = $('.person').length; console.log(personLength); #> 7 $("[data-action=like]").first().click()
的计数减少一 it("should show one less person if you tap you liked them", function() {
var personLength = $('.person').length;
console.log(personLength); #> 7
$("[data-action=like]").first().click();
console.log($('.person').length); #> 7
console.log(Likes.find().fetch()); #> 1
expect($('.person').length).toEqual(person-1); #> Fail (expected 7 to equal 6)
});
我不明白它为什么会这样。在手动测试时,我显然得到了预期的结果
我想我错过了一些方法来重新设置测试,以便再次查看DOM还是什么?也许是某个异步回调方法?我不确定,但似乎只是一个简单的错误。控制反应性
首先你应该了解反应性和流星是如何工作的。
管理反应性的组件称为跟踪器(以前称为Deps)。
您可以在中阅读它的工作原理
每次触发会导致反应性行为和
如果要测试反应行为的结果,应该
触发操作后调用Tracker.flush()。这将确保
在评估您的期望之前,将应用所有反应性更改
何时需要调用Tracker.flush()
?(列表不完整)
- 使用
和Blaze.render
Blaze.renderWithData
- 触发DOM事件后
- 更改集合中的数据后
Tracker.flush()
对于您的示例,这应该可以做到:
beforeAll(function () {
var self = this;
self.deferAfterFlush = function (callback) {
Tracker.afterFlush(function () {
Meteor.defer(callback);
});
};
});
// Guarantee that tests don't run in a ongoing flush cycle.
beforeEach(function (done) {
this.deferAfterFlush(done);
});
it("should show one less person if you tap you liked them", function() {
var personLength = $('.person').length;
console.log(personLength); #> 7
$("[data-action=like]").first().click();
Tracker.flush();
console.log($('.person').length); #> 6
console.log(Likes.find().fetch()); #> 1
expect($('.person').length).toEqual(person-1); #> Pass (expected 6 to equal 6)
});
Likes.find().fetch()是异步的吗?