0

我正在编写一个支持 Meteor Tracker 的模块,但我无法对其进行测试。我添加meteor-standalone-tracker为开发依赖项,并编写了一个测试用例,但该autorun函数仅调用一次。

例如,我写了一个受文档启发的虚拟测试,如下所示

import assert from 'assert';
import Tracker from 'trackr';

describe('Testing Tracker', () => {

  it('should do as expected', () => {

    var favoriteFood = "apples";
    var favoriteFoodDep = new Tracker.Dependency;

    var getFavoriteFood = function () {
      favoriteFoodDep.depend();
      return favoriteFood;
    };

    var setFavoriteFood = function (newValue) {
      favoriteFood = newValue;
      favoriteFoodDep.changed();
    };

    console.log("GET:" , getFavoriteFood());
    // "apples"

    var handle = Tracker.autorun(function () {
      console.log("Your favorite food is " + getFavoriteFood());
    });
    // "Your favorite food is apples"

    setFavoriteFood("mangoes");
    // "Your favorite food is mangoes"
    setFavoriteFood("peaches");
    // "Your favorite food is peaches"
    setFavoriteFood("bananas");
    // "Your favorite food is bananas"
    handle.stop();
    setFavoriteFood("cake");
    // (nothing printed)

  });

});

我得到的唯一输出是:

console.log test/tracker.spec.js:27
   GET: apples

console.log test/tracker.spec.js:31
   Your favorite food is apples

我错过了什么?

4

1 回答 1

0

我目前的解决方案是刷新所有计算。

Tracker.flush();

这将强制autorun调用具有已更改依赖项的函数。

于 2018-09-07T21:51:28.887 回答