如何对fork进行单元测试

时间:2019-09-20 17:10:05

标签: angular rxjs observable karma-jasmine

我有一个利用forkJoin的代码,

std::function<int(int, Foo const &, Bar &)>  sh{ SomeHandler };

RegisterCmdHandler(1, sh);

如何模拟订阅,以便还可以评估其中的代码。

我尝试了以下类似的操作

forkJoin(serviceRequests).subscribe((forkResults) => {
                //Do stuff with forkResults here 
});

1 个答案:

答案 0 :(得分:0)

//首先,我假设您在

之类的函数中使用forkJoin
  func() {
    forkJoin(this.getForkedStreams()).pipe(takeUntil(this.unsubscribe)).subscribe((streams: any[]) => {
      console.log(streams[0]);
      console.log(streams[1]);
      console.log(streams[2]);

      // logs the array objects provided by the streams
    });
  }
  getForkedStreams() {
    return [
      this.service.func1(),
      this.service.func2(),
      this.service.func3()
    ];
  }

//然后在spec文件中将流数组模拟为

//模拟如下服务功能:

     let mockService: any;
    mockService = {
  func1: () => {
    return of({
      prop: 'xyz'
    });
  },
  func2: () => {
    return of({
      prop: 'xyz'
    });
  },
  func3: () => {
    return of({
      prop: 'xyz'
    });
  }
};

//服务注入         新的SampleComponent(mockService任意);

   /*
Then, in the actual describe => it block, assert against the values mocked up with the variable with which it would be assigned to.
I am not doing that since it is only the mocking that you've asked and you might want to continue from there
*/
相关问题