我正在编写一个支持 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
我错过了什么?