Meteor Velocity集成Jasmine测试框架未返回预期结果?

18

我正在尝试测试以下内容,该内容在手动情况下有效:

  1. 返回用户列表作为<div>
  2. 单击按钮以将<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或其他东西的方法?也许需要某些异步方法来回调?我不确定,但似乎是一个简单的错误。

Likes.find().fetch() 是异步的吗? - Michael Radionov
1个回答

12

控制响应性

首先,您应该了解响应性和Meteor的工作原理。管理响应性的组件称为Tracker(以前是Deps)。您可以在Meteor手册中了解其工作原理。

每次触发会导致响应行为的操作并且您想要测试响应行为的结果时,您应该在触发操作后调用Tracker.flush()。这将确保在评估您的期望之前应用所有响应性更改。

什么情况下需要调用Tracker.flush()?(不完整列表)

  • 在使用Blaze.renderBlaze.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)
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接