测试角度效果CatchError

时间:2019-02-13 12:15:26

标签: angular testing jasmine ngrx

我正在尝试测试一个效果,但是当catchError时我不知道如何处理。

效果

@Effect()
signup$: Observable<Action> = this.actions$.pipe(
    ofType(AuthActionTypes.SIGNUP),
    map((action: Signup) => action.payload),
    switchMap((user: any) => {
        this.userSignup = user;
        return this.createUserGQL.mutate({ userInput: user });
    }),
    switchMap(() => {
        return this.signInGQL.watch({
            email: this.userSignup.email,
            password: this.userSignup.password
        }).valueChanges.pipe(map((login: any) => {
            const authData = login.data.login;
            return authData;
        }));
    }),
    mergeMap((res: any) => {
        const token = res.token;
        this.localStorageService.setItem(AUTH_KEY, { token: token, isAuthenticated: true });
        return [new SignupSuccess, new SetToken(token)];
    }),
    catchError(err => {
        return of(new RetrieveError({ error: err }));
    })
);

规格

it('should emit RetrieveError on error', () => {
        const userData: UserInputData = { name: 'test', email: 'test@test.com', password: 'secretsecret' };
        const signupAction = new Signup(userData);
        const error = 'ERROR';
        const errorAction = new RetrieveError({
            error: error
        });
        const valueSource = {
            a: signupAction
        };
        const valueSourceError = {
            e: errorAction
        };
        const source = cold('a', valueSource);
        const expected = cold('e', valueSourceError);
        const actions = new Actions(source);

        createUserGQL.mutate.and.returnValue(throwError(error));

        const effect = new AuthEffects(actions, router, localStorageService, createUserGQL, signInGQL);

        expect(effect.signup$).toBeObservable(expected);
    });

错误 为什么效果有长度2? [预期$ .length = 2等于1。 数组中有意外的$ {1 =对象({帧:0,通知:Notification({种类:'C',值:undefined,错误:undefined,hasValue:false})}))。1

0 个答案:

没有答案