如何专门测试登录异常。
submit() {
const { username, password } = this.loginForm.value;
this.store.dispatch(new Login({ username, password }))
.subscribe(
() => {
},
catchError(() => throwError(new Error(ToastCode.LOGIN_FAILED)))
);
}
到目前为止,我的测试仍然有效,但是代码覆盖范围告诉我也要测试错误处理。
到目前为止,我的测试是
it('should submit the login form', () => {
const data = {
username: 'test@test.de',
password: 'test123'
} as Credentials;
component.loginForm.patchValue({ ...data });
component.submitLoginForm();
expect(store.dispatch).toHaveBeenCalledWith(
new Login({ ...data })
);
});
但是我不知道如何添加一个检查异常的测试。
答案 0 :(得分:0)
您可以为此使用功能const filtered = array1.filter(item => !Object.keys(item).includes('roma'));
。
答案 1 :(得分:0)
我想你在 store.dispatch 上有一个间谍,如果可以的话
mySpy.and.throwError('error ...')
然后您可以使用
expect(submit()).toThrow(TypeError);
在测试中调用 submit()后