我正在尝试测试以下手动工作:
<div>
的<div>
的计数是一个。 这似乎不起作用:
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 或其他什么?也许一些异步方法来回调?我不确定,但似乎是一个简单的错误。
最佳答案
控制 react 性
首先,您应该了解 react 性和 Meteor 的工作原理。
管理响应性的组件称为 Tracker(以前称为 Deps)。
您可以在 Meteor Manual 中阅读它的工作原理。 .
每次触发会导致 react 性行为的 Action 时,
你想测试 react 行为的结果,你应该
调用Tracker.flush()
触发 Action 后。这将确保
在您评估您的期望之前,将应用所有被动更改。
什么时候是 Tracker.flush()
需要打电话吗? (不完整列表)
Blaze.render
渲染模板后和 Blaze.renderWithData
如果您的期望失败并且您已手动验证测试
行为有效,您可以尝试插入
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)
});
关于javascript - Jasmine 的 meteor 速度没有返回预期结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31910046/