很抱歉打扰您,但我很难受
了解测试效果背后的逻辑:(
任何人都可以给我展示和解释一下
如何测试此Auth登录效果?
我也在寻找一些有用的教程:)
def set_values(x,y):
x,y=6,7
return(x,y)
a,b=0,0
a,b = set_values(a,b)
# a = 6 b =7
import { Injectable } from '@angular/core';
import { of } from 'rxjs';
import { map, switchMap, catchError } from 'rxjs/operators';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Credentials, AuthToken } from '../../models';
import { Go } from '../../../router';
import { AuthService } from '../../services';
import { AuthActionTypes, Login, LoginFailure, LoginSuccess } from '../actions';
@Injectable()
export class AuthEffects {
@Effect()
login$ = this.actions$.pipe(
ofType(AuthActionTypes.Login),
map((action: Login) => action.payload.credentials),
switchMap((credentials: Credentials) => {
return this.service.login(credentials).pipe(
map((token: AuthToken) => new LoginSuccess({ token })),
catchError(error => of(new LoginFailure(error)))
);
})
);
@Effect()
loginSuccess$ = this.actions$.pipe(
ofType(AuthActionTypes.LoginSuccess),
map(() => new Go({ path: ['/admin'] }))
);
constructor(private actions$: Actions, private service: AuthService) {}
}
测试失败并显示消息
// Testing
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { TestBed } from "@angular/core/testing";
import { provideMockActions } from "@ngrx/effects/testing";
// Rxjs
import { Observable } from "rxjs";
// Marbles
import { hot, cold } from "jasmine-marbles";
// Store
import { Credentials, AuthToken } from "../../models";
import { CONFIG, Config } from "../../../../config/config.module";
import { AuthService } from "../../services";
import { AuthEffects } from "./auth.effects";
import * as fromActions from "../actions";
const credentials: Credentials = {
email: "io@io.io",
password: "abc"
};
const error: any = { status: 401, message: "401 Unauthorized Error" };
const token: AuthToken = {
token: "abc",
expiresIn: 1546356028904
};
describe("AuthEffects", () => {
let effects: AuthEffects;
let actions: Observable<any>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
AuthService,
{
provide: CONFIG,
useClass: Config
},
AuthEffects,
provideMockActions(() => actions)
]
});
effects = TestBed.get(AuthEffects);
});
it("should work", () => {
const action = new fromActions.Login({ credentials });
const completion = new fromActions.LoginSuccess({ token });
actions = hot("a", { a: action });
const expected = cold("b", { b: completion });
expect(effects.login$).toBeObservable(expected);
});
});
答案 0 :(得分:1)
也许您应该在预期的可观察值中使用“ -b”而不是“ b”:
常量期望=寒冷(“ -b”,{b:完成});
以下是与您的情况类似的示例:https://github.com/blove/ngrx-testing/blob/master/src/app/state/user/user.effects.spec.ts#L54