相关疑难解决方法(0)

在RxJS 5.5中测试和模拟可调运算符

在lettable operator之前,我做了一个帮助来修改debounceTime方法,所以它使用了一个TestScheduler:

export function mockDebounceTime(
    scheduler: TestScheduler,
    overrideTime: number,
): void {
    const originalDebounce = Observable.prototype.debounceTime;

    spyOn(Observable.prototype, 'debounceTime').and.callFake(function(
        time: number,
    ): void {
        return originalDebounce.call(
            this,
            overrideTime,
            scheduler,
        );
    });
}
Run Code Online (Sandbox Code Playgroud)

因此,以下Observable的测试很简单:

@Effect()
public filterUpdated$ = this.actions$
    .ofType(UPDATE_FILTERS)
    .debounceTime(DEFAULT_DEBOUNCE_TIME)
    .mergeMap(action => [...])
Run Code Online (Sandbox Code Playgroud)

对于lettable运算符,filterUpdated $ Observable的编写方式如下:

@Effect()
public filterUpdated$ = this.actions$
    .ofType(UPDATE_FILTERS)
    .pipe(
        debounceTime(DEFAULT_DEBOUNCE_TIME),
        mergeMap(action => [...])
    );
Run Code Online (Sandbox Code Playgroud)

我不能修复debounceTime运算符了!如何将testScheduler传递给debounceTime运算符?

typescript rxjs5 ngrx-effects

9
推荐指数
1
解决办法
3244
查看次数

如何在零时间内获取 RxJS Observable 事件?

我正在将 Observable 的所有事件收集到一个data数组中:

const obs$ = Rx.Observable
  .interval(500)
  .take(4);

let data = [];
const start = performance.now();

obs$.subscribe(
  value => {
    data.push({
      time: performance.now() - start,
      data: value
    });
  },
  () => {},
  () => {
    console.log(JSON.stringify(data, null, 2));
  }
);
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/rxjs@5.2.0/bundles/Rx.js"></script>
Run Code Online (Sandbox Code Playgroud)

是否有可能“预见未来”并在不等待 2 秒的情况下获得相同的data数组?

澄清一下,我试图找到一种方法,以某种方式obs$用自定义计时器/调度程序以某种方式包装给定的 Observable(在上面的示例中),以便我可以立即获取事件。

javascript rxjs rxjs5

4
推荐指数
1
解决办法
1251
查看次数

为什么jest.useFakeTimers不能使用RxJs Observable延迟

我想知道为什么jest.useFakeTimerssetTimeoutRxJs的延迟运算符一起工作但不是:

jest.useFakeTimers();
import {Observable} from 'rxjs/Observable';
import 'rxjs';

describe('timers', () => {
    it('should resolve setTimeout synchronously', () => {
        const spy = jest.fn();
        setTimeout(spy, 20);
        expect(spy).not.toHaveBeenCalled();
        jest.runTimersToTime(20);
        expect(spy).toHaveBeenCalledTimes(1);
    });
    it('should resolve setInterval synchronously', () => {
        const spy = jest.fn();
        setInterval(spy, 20);
        expect(spy).not.toHaveBeenCalled();
        jest.runTimersToTime(20);
        expect(spy).toHaveBeenCalledTimes(1);
        jest.runTimersToTime(20);
        expect(spy).toHaveBeenCalledTimes(2);
    });
    it('should work with observables', () => {
        const delay$ = Observable.of(true).delay(20);
        const spy = jest.fn();
        delay$.subscribe(spy);
        expect(spy).not.toHaveBeenCalled();
        jest.runTimersToTime(2000);
        expect(spy).toHaveBeenCalledTimes(1);
    });
});
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

仅供参考:使用20或2000作为参数jest.runTimersToTime没有任何区别.使用jest.runAllTimers()使测试过去

javascript unit-testing rxjs jestjs asynctest

3
推荐指数
1
解决办法
1316
查看次数

如何在计时器触发后正确调用函数进行单元测试。角度 7、RXJS 6

在我的 Angular 7 应用程序中,我有一个用于跟踪活动用户任务的服务。在该服务中,计时器每秒运行一次,以检查是否有任何任务在 30 秒内仍未完成。如果发现任何任务已过期,则该任务将通过服务上的事件发射器发出,以便在其他地方处理。当应用程序在浏览器中运行时,这一切都有效,但是当我尝试编写单元测试来测试 fakeAsync 环境中的行为时,tick(X)不会提前时间(或者 fakeAsync 不会模拟任何“new Date()”的时间' 在服务中创建以tick()正常工作)。

由于我是角度单元测试的新手,我也承认问题可能是我如何设置测试(事实上,我怀疑这就是问题所在)。

我发现许多关于旧版本 Angular 的帖子都存在 Date 未正确模拟的问题,因此建议的解决方法是使用 asyncScheduler 绕过勾选或导入其他 npm 包,或者,我什至尝试了来自区。我已经尝试过这些但没有成功。我还通过运行下面的简单测试来测试fakeAsync()tick()功能,该测试通过了:@angular/core/testing

  it('should tick', fakeAsync(() => {
    const firstDate = new Date();
    tick(30000);
    const secondDate = new Date();
    expect(secondDate.getTime() - firstDate.getTime()).toBe(30000);
  }));
Run Code Online (Sandbox Code Playgroud)

这是该服务的简化版本:

export class UserTaskTrackerService {
  TaskExpired = new EventEmitter<UserTask>

  private activeUserTasks: UserTask[] = []
  private oneSecondTimer;
  private timerSubscription$;

  constructor() {
    this.oneSecondTimer = timer(1000, 1000);
    this.timerSubscription$ = this.oneSecondTimer.subscribe(() …
Run Code Online (Sandbox Code Playgroud)

jasmine rxjs angular

3
推荐指数
1
解决办法
5188
查看次数