gpt4 book ai didi

javascript - 在另一个项目中使用 marble testing rxjs5 方法

转载 作者:数据小太阳 更新时间:2023-10-29 04:47:29 25 4
gpt4 key购买 nike

我有一个 webapp 项目,它使用 rxjs5 来实现 flux我目前正在寻找为其编写单元测试的解决方案。

其实我已经在里面实现了自定义的observables,例如:

function getActivityObservable(events, timeout) {
return Observable.create((observer) => {
const deb = debounce(() => observer.next(false), timeout || DEFAULT_TIMEOUT);
const sub = events.subscribe((e) => {
if (!e) {
deb.cancel();
observer.next(false);
} else {
observer.next(true);
deb(e);
}
});

return () => {
if (sub) sub.unsubscribe();
if (deb) deb.cancel();
};
}).distinctUntilChanged();
}

我想用 marble testing way 测试它并写下类似的东西(我从 rxjs 存储库中获取了一个示例)

  describe("getActivityObservable", () => {
it("should debounce by selector observable", () => {
const e1 = hot("--a--bc--d----|");
const e1subs = "^ !";
const expected = "----a---c--d--|";

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

我的问题是:

是否可以在 rxjs5 项目之外使用大理石测试方法(使用 hotcold 等运算符...)。我不知道如何在我的项目中使用这个好工具。

感谢您的帮助。

最佳答案

你可以但作为 Ben评论:“这不是很符合人体工程学”。

我正在使用 mocha 和 monkey patching it:

const isEqual = require('lodash.isequal');
const TestScheduler = require('rxjs/testing/TestScheduler').TestScheduler;

const assertDeepEqualFrame = (actual, expected) => {
if (!isEqual(actual, expected)) {
throw new Error('Frames not equal!');
}
}

const oit = global.it;
global.it = function(description, cb, timeout) {
if (cb.length === 0) {
oit(description, function() {
global.rxTestScheduler = new TestScheduler(assertDeepEqualFrame);
cb();
global.rxTestScheduler.flush();
});
} else { // async test
oit.apply(this, arguments);
}
};

我从 ngrx/store 中获得了很多灵感特别是这个文件:https://github.com/ngrx/store/blob/master/spec/helpers/test-helper.ts

然后我可以像这样编写测试吗:

it('should filter with an always-true predicate', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '--3-4-5-6--7-8--9--|';
const predicate = () => { return true; };

expectObservable(source.filter(predicate)).toBe(expected);
});

编辑你可以在这里看到我是如何猴子修补 it 的:https://github.com/tjoskar/ng2-lazyload-image/blob/5e1c64a3611530ce26857a566b2d76dff890a3c5/test/helpers/test-helper.ts

关于javascript - 在另一个项目中使用 marble testing rxjs5 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36979451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com