Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.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
Javascript 当returm实际上是一个空的可观察对象时,如何使用MABLE方法进行测试?_Javascript_Jasmine_Rxjs5_Ngrx Effects_Rxjs Marbles - Fatal编程技术网

Javascript 当returm实际上是一个空的可观察对象时,如何使用MABLE方法进行测试?

Javascript 当returm实际上是一个空的可观察对象时,如何使用MABLE方法进行测试?,javascript,jasmine,rxjs5,ngrx-effects,rxjs-marbles,Javascript,Jasmine,Rxjs5,Ngrx Effects,Rxjs Marbles,我使用rxjs中的EMPTY来处理catchError,期望的正确值是多少才能通过失败场景 import { Injectable } from '@angular/core'; import { Actions, createEffect, ofType } from '@ngrx/effects'; import { EMPTY } from 'rxjs'; import { map, mergeMap, catchError } from 'rxjs/operators'; import

我使用rxjs中的EMPTY来处理catchError,期望的正确值是多少才能通过失败场景

import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { EMPTY } from 'rxjs';
import { map, mergeMap, catchError } from 'rxjs/operators';
import { MoviesService } from './movies.service';

@Injectable()
export class MovieEffects {

  loadMovies$ = createEffect(() => this.actions$.pipe(
    ofType('[Movies Page] Load Movies'),
    mergeMap(() => this.moviesService.getAll()
      .pipe(
        map(movies => ({ type: '[Movies API] Movies Loaded Success', payload: movies })),
        catchError(() => EMPTY)
      ))
    )
  );

  constructor(
    private actions$: Actions,
    private moviesService: MoviesService
  ) {}
}



我自己也遇到了这个问题,偶然发现了你的问题。我想我有一个答案:既然
空了
我就认为它意味着0个时间过去了。(编辑:这是错误的!)

EMPTY
EMPTY()
本身将匹配
cold(“|”)
hot(“|”)
。请阅读下面的内容,了解其与效果中的
cold(“”)
hot(“”)
匹配的原因。(请参见的“示例”部分,其中也显示了这一点。)

经a确认,现引用原因:

cold(“|”)
中的管道字符表示可观察流的完成。但是,您的效果将不会完成。
empty()
observable确实完成了,但是从
switchMap
返回
empty()
只会看到该observable合并到效果observable的流中-它不会完成效果observable

因此,虽然
EMPTY
(或
EMPTY()
)被记录为立即完成,但在效果中使用它的最终结果表明效果永远不会完成

将此答案插入您的示例:

it('should return a empty observable', () => {
   this.moviesServiceSpy.and.return(throwError('Error in service'));

   action$ = hot('a', a: { loadMovies() });

   const expected = cold('');

   expect(loadMovies$).toBeObservable(expected);

})
为了好玩,以下测试也通过了:

it('should match EMPTY with a single tick pipe marble', () => {
   expect(EMPTY).toBeObservable(cold('|'));
   expect(EMPTY).toBeObservable(hot('|'));

   // empty() is deprecated, but these assertions still pass.
   expect(empty()).toBeObservable(cold('|'));
   expect(empty()).toBeObservable(hot('|'));
});
it('should match EMPTY with a single tick pipe marble', () => {
   expect(EMPTY).toBeObservable(cold('|'));
   expect(EMPTY).toBeObservable(hot('|'));

   // empty() is deprecated, but these assertions still pass.
   expect(empty()).toBeObservable(cold('|'));
   expect(empty()).toBeObservable(hot('|'));
});