Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度单元测试/模拟连接_Angular_Unit Testing_Rxjs_Fork Join - Fatal编程技术网

Angular 角度单元测试/模拟连接

Angular 角度单元测试/模拟连接,angular,unit-testing,rxjs,fork-join,Angular,Unit Testing,Rxjs,Fork Join,我正在Angular 9中使用Jest&Occessor并尝试为组件编写单元测试。 问题是,在我的ngOnInit()中,我有一个forkJoin调用我的facades,然后在subscribe中,将返回的值分配给正确的变量 当我的测试运行时,我收到以下错误: TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iter

我正在Angular 9中使用Jest&Occessor并尝试为组件编写单元测试。 问题是,在我的ngOnInit()中,我有一个forkJoin调用我的facades,然后在subscribe中,将返回的值分配给正确的变量

当我的测试运行时,我收到以下错误:

TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

      81 |       myData1: this.facade1.findThisThing(),
      82 |       myData2: this.facade2.findThatThing(),
    > 83 |     }).subscribe(data => {
         |        ^
      84 |       this.data1 = data.myData1.values;
      85 |       this.data2 = data.myData2.values;
我如何模拟这个订阅/处理它

组成部分:


ngOnInit(): void {
    forkJoin({
      myData1: this.facade1.findThisThing(),
      myData2: this.facade2.findThatThing(),

    }).subscribe(data => {
      this.data1 = data.myData1.values;
      this.data2 = data.myData2.values;
    });
}
当我通过createComponentFactory方法创建组件时,我想把它放在providers数组中的component.spec中,但这似乎没有帮助

组件规范:

const createComponent = createComponentFactory<ComponentUnderTest>(
    {
      component: ComponentUnderTest,
      mocks: [
       ...
      ]
      providers: [
        {
          provide: 'subscribe',
          useValue: jest.fn()
        }
      ],
      shallow: true,
    },
  );
const createComponent=createComponentFactory(
{
组件:测试中的组件,
模拟:[
...
]
供应商:[
{
提供:'订阅',
useValue:jest.fn()
}
],
肤浅:是的,
},
);
任何帮助都将不胜感激


非常感谢堆栈溢出社区

在forkjoin中阅读有关间谍和模拟两种方法的内容。 差不多

spyOn(this.facade1.findThisThing)和.returnValue(of())


secondMethod也是如此。

问题实际上不是subscribe语句本身。您要做的是模拟服务,从而模拟在forkJoin中使用的函数的返回值。请注意,rxjs是尽可能同步的,因此如果您要返回使用操作符的
创建的可观察对象,那么它是完全同步的。因此,您可以模拟该函数,并利用
操作符的
返回一个可观察的值

您面临的问题实际上正在发生,因为传递给forkJoin的函数之一返回
未定义的
。请注意,如果您正在利用jasmine spy模拟Angular服务,如果您没有指定函数的返回值,那么您正在监视,它将返回未定义的值