【问题标题】:Meteor Velocity with Jasmine not returning expecting result?茉莉花的流星速度没有返回预期结果?
【发布时间】:2015-11-01 19:20:42
【问题描述】:

我正在尝试手动测试以下内容:

  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() 是异步的吗?

标签: javascript meteor jasmine velocity


【解决方案1】:

控制反应性

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

每次触发会导致反应性行为的动作时, 你想测试反应行为的结果,你应该 触发动作后调用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)
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2020-05-07
    • 2017-02-10
    • 2018-03-02
    • 2014-12-12
    相关资源
    最近更新 更多