使用 angular 进行测试时如何使用 'ngrx effects' 触发错误
How to trigger an error with 'ngrx effects' when testing with angular
这是我的效果:
getAssessmentPeriods$: Observable<Action> = createEffect(() =>
this.actions$.pipe(
ofType(courseAttributeActions.getAssessmentPeriod_CR),
switchMap(() => this.courseAttributeService.getAssessmentPeriods()
.pipe(
map((value: any) => {
return courseAttributeActions.getAssessmentPeriodSuccess_CR({ payload: value });
}),
catchError(error => of(courseAttributeActions.LoadFailure_CR(error)))
))
));
规范文件:快乐场景工作正常:
it('should return a cold observable of getAssessmentPeriodSuccess_CR', () => {
const fakeData: any = [];
const start = courseAttributeActions.getAssessmentPeriod_CR();
const completion = courseAttributeActions.getAssessmentPeriodSuccess_CR({ payload: fakeData });
jest.spyOn(service, 'getAssessmentPeriods').mockImplementation(() => of(fakeData));
actions = hot('--a-', { a: start });
const expected = cold('--(b)', { b: completion });
expect(effects.getAssessmentPeriods$).toBeObservable(expected);
});
规范文件:错误场景不起作用:
it('should return a cold observable of LoadFailure_CR error ', () => {
const errors: any = throwError((new Error('Network error')));
const start = courseAttributeActions.getAssessmentPeriod_CR();
const completionError = courseAttributeActions.LoadFailure_CR({error:errors});
jest.spyOn(service, 'getAssessmentPeriods').mockImplementation(() => of(errors));
actions = hot('--a-', { a: start });
const expected = cold('--(b)', { b: completionError });
expect(effects.getAssessmentPeriods$).toBeObservable(expected);
});
在错误情况下如何触发服务中的error
?实际上我是从间谍发来的。但我仍然收到成功消息。
控制台出现如下错误:
- Expected
+ Received
@@ -4,14 +4,14 @@
"notification": Notification {
"error": undefined,
"hasValue": true,
"kind": "N",
"value": Object {
- "error": Observable {
+ "payload": Observable {
"_isScalar": false,
"_subscribe": [Function anonymous],
},
- "type": "[candidate-registration-management] LoadFailure_CR",
+ "type": "[candidate-registration-management_CR] getAssessmentPeriodSuccess",
},
},
},
]
69 | const expected = cold('--(b)', { b: completionError });
70 |
> 71 | expect(effects.getAssessmentPeriods$).toBeObservable(expected);
如何修复错误?是否真的需要为 1 个效果编写 2 个单独的 'it' 函数?
寻求帮助。提前致谢。
throwError
等同于
return new Observable(subscriber => subscriber.error(error));
因此,通过执行 mockImplementation(() => of(errors));
,您将拥有一个可观察对象(例如 of
),它会发出另一个可观察对象(next
通知)。
为了使错误发生,getAssessmentPeriods
observable 必须发出 error
通知,所以我认为您可以通过以下方式解决它:
.mockImplementation(() => errors)
这是我的效果:
getAssessmentPeriods$: Observable<Action> = createEffect(() =>
this.actions$.pipe(
ofType(courseAttributeActions.getAssessmentPeriod_CR),
switchMap(() => this.courseAttributeService.getAssessmentPeriods()
.pipe(
map((value: any) => {
return courseAttributeActions.getAssessmentPeriodSuccess_CR({ payload: value });
}),
catchError(error => of(courseAttributeActions.LoadFailure_CR(error)))
))
));
规范文件:快乐场景工作正常:
it('should return a cold observable of getAssessmentPeriodSuccess_CR', () => {
const fakeData: any = [];
const start = courseAttributeActions.getAssessmentPeriod_CR();
const completion = courseAttributeActions.getAssessmentPeriodSuccess_CR({ payload: fakeData });
jest.spyOn(service, 'getAssessmentPeriods').mockImplementation(() => of(fakeData));
actions = hot('--a-', { a: start });
const expected = cold('--(b)', { b: completion });
expect(effects.getAssessmentPeriods$).toBeObservable(expected);
});
规范文件:错误场景不起作用:
it('should return a cold observable of LoadFailure_CR error ', () => {
const errors: any = throwError((new Error('Network error')));
const start = courseAttributeActions.getAssessmentPeriod_CR();
const completionError = courseAttributeActions.LoadFailure_CR({error:errors});
jest.spyOn(service, 'getAssessmentPeriods').mockImplementation(() => of(errors));
actions = hot('--a-', { a: start });
const expected = cold('--(b)', { b: completionError });
expect(effects.getAssessmentPeriods$).toBeObservable(expected);
});
在错误情况下如何触发服务中的error
?实际上我是从间谍发来的。但我仍然收到成功消息。
控制台出现如下错误:
- Expected
+ Received
@@ -4,14 +4,14 @@
"notification": Notification {
"error": undefined,
"hasValue": true,
"kind": "N",
"value": Object {
- "error": Observable {
+ "payload": Observable {
"_isScalar": false,
"_subscribe": [Function anonymous],
},
- "type": "[candidate-registration-management] LoadFailure_CR",
+ "type": "[candidate-registration-management_CR] getAssessmentPeriodSuccess",
},
},
},
]
69 | const expected = cold('--(b)', { b: completionError });
70 |
> 71 | expect(effects.getAssessmentPeriods$).toBeObservable(expected);
如何修复错误?是否真的需要为 1 个效果编写 2 个单独的 'it' 函数? 寻求帮助。提前致谢。
throwError
等同于
return new Observable(subscriber => subscriber.error(error));
因此,通过执行 mockImplementation(() => of(errors));
,您将拥有一个可观察对象(例如 of
),它会发出另一个可观察对象(next
通知)。
为了使错误发生,getAssessmentPeriods
observable 必须发出 error
通知,所以我认为您可以通过以下方式解决它:
.mockImplementation(() => errors)