【问题标题】:Execute logic inside RxJS pipe with unit tests使用单元测试在 RxJS 管道中执行逻辑
【发布时间】:2020-09-16 16:25:21
【问题描述】:

在进行单元测试时尝试在 pipe() 方法中初始化变量 x,但它没有被调用。如何在 pipe() 中运行方法调用/初始化逻辑?在单元测试中简单地调用component.ngOnInit() 并没有帮助。

ngOnInit() {
    this.foo$ = this.myService.bar$.pipe(
      ....
      map(bar => {
        this.x = bar.x;        // how do I call this within unit test 
        return bar;
      })
    );  
}

it('should init x', () => {
    component.ngOnInit();
    expect(component.x).toEqual(bar.x); //fails
});

【问题讨论】:

    标签: angular unit-testing rxjs jasmine karma-jasmine


    【解决方案1】:

    你可能想订阅然后打勾

      import { fakeAsync, tick } from '@angular/core/testing'; 
    
      it('should init x', fakeAsync(() => {
        component.ngOnInit();
        component.foo$.subscribe();
        tick();
        expect(component.x).toEqual(bar.x); 
      }));
    

    【讨论】:

    • 试过了,但它只是跳过了 .pipe() 中的代码。
    猜你喜欢
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    相关资源
    最近更新 更多