问题描述
目标:我想为基本的NgRx效果编写适当的单元测试。到目前为止,使用茉莉花大理石时我的尝试都没有成功。
这是我要测试的效果:
markMovieAsFinished$ = createEffect(() =>
this.actions$.pipe(
ofType(MovieActions.markMovieAsFinished),switchMap((action: Action) => {
return this.http.put(`/api/reading-list/${action.item.movieId}/finished`,{ finishedDate: action.item.finishedDate })
.pipe(
switchMap(() => EMPTY) // With an optimistic update,we initially handle the 'markMovieAsFinished' action directly in the reducer,hence,no need to dispatch another action on a successful PUT request!
);
})
)
);
这是我最近一次使用大理石测试的尝试。 (我在实施大理石测试方面非常陌生!):
describe('MovieEffects',() => {
let actions$: Actions;
let effects: MovieEffects;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ NxModule.forRoot(),SharedTestingModule ],providers: [
MovieEffects,DataPersistence,provideMockActions(() => actions$)
]
});
actions$ = TestBed.inject(Actions);
effects = TestBed.inject(MovieEffects);
httpMock = TestBed.inject(HttpTestingController);
});
describe('markMovieAsFinished$',() => {
it('should return an EMPTY observable with the outcome,on success',() => {
const action: Action = MovieActions.markMovieAsFinished({ finishedDate: '1/1/20'});
const completion = EMPTY;
// setup the Effect
actions$ = hot('-a',{ a: action });
const response = cold('-b|',{ b: {} }); // payload from successful response can just be an empty object
const expected = cold('--c',{ c: completion });
expect(effects.markMovieAsFinished$).to.be.eql(expected);
})
});
当我尝试上述尝试时,markMovieAsFinished $值返回一个空数组。在上面的代码中,我已经设置了单元测试的安排和声明部分。我不了解的部分是如何模拟成功的PUT请求以测试这种效果。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)