我正在使用ng-rx和AngularFire进行项目。我正在使用自定义令牌登录。这是效果的代码:
@Effect()
custom$ = this.actions$.pipe(
ofType(fromAuthActions.AuthActionTypes.CustomTokenReceived),
switchMap((action: fromAuthActions.CustomTokenReceived) => {
return fromPromise(this.auth.auth.signInWithCustomToken(action.payload.token)).pipe(
map((info: UserCredential) => new fromAuthActions.LoginSuccess()),
tap(a => console.log(a)),
catchError(error => of(new fromAuthActions.LoginFailed(error)))
);
})
);
然后我要对该效果进行单元测试:
....
const angularFireAuthStub = {
auth: {
signInWithCustomToken: jasmine.createSpy('signInWithCustomToken').and.returnValue(Promise.resolve({dd: ''}))
}
};
....
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
AuthService,
provideMockActions(() => actions),
{provide: AngularFireAuth, useValue: angularFireAuthStub},
AuthEffects
],
});
....
it('should authorize on fb with custom token and return a login successful action', () => {
const action = new fromActions.CustomTokenReceived({token: '12345'});
const completion = new fromActions.LoginSuccess();
actions = hot('-a', {a: action});
const expected = cold('-b', {b: completion});
expect(effects.custom$).toBeObservable(expected);
});
运行此命令时,我得到:
Expected $.length = 0 to equal 1.
Expected $[0] = undefined to equal Object({ frame: 10, notification: Notification({ kind: 'N', value: LoginSuccess({ type: '[Auth] Login Success' }), error: undefined, hasValue: true }) }).
我已经通过添加console.log调用验证了signInWithCustomToken返回预期的模拟值。我想知道为什么在toBeObservable期望中未检测到。有什么想法吗?
答案 0 :(得分:0)
似乎无法使用弹珠Observable.fromPromise empty during unit testing测试fromPromise
所以我将测试更改为:
it('should authorize on fb with custom token and return a login successful action', () => {
const action = new fromActions.CustomTokenReceived({token: '12345'});
const completion = new fromActions.LoginSuccess();
actions = hot('-a', {a: action});
effects.custom$.subscribe(r => expect(r).toEqual(completion));
});