在对 Angular 服务进行单元测试时模拟 AngularFireAuth
Mock AngularFireAuth When Unit Testing an Angular Service
我有一个 authService
实例化时订阅 AngularFireAuth
's Observable authState
and sets the services' 内部(私有)属性 authState
.
所以我可以进行单元测试 authService
我在我的测试规范中用 Reflect.get/set
劫持了服务的内部 authState
这样我就可以控制它的值。
问题当然是 authService
在其实例化期间仍然订阅 AngularFireAuth
的 Observable authState
而我没有想要,也不需要。
我 假设 我需要模拟 AngularFireAuth,它伪造订阅并且实际上不与 Firebase 通信?单元测试的新手,我不知道该怎么做。
auth.service.ts
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class AuthService {
private authState: firebase.User;
constructor(private afAuth: AngularFireAuth) { this.init(); }
private init(): void {
this.afAuth.authState.subscribe((authState) => {
if (authState === null) {
this.afAuth.auth.signInAnonymously()
.then((authState) => {
this.authState = authState;
})
.catch((error) => {
throw new Error(error.message);
});
} else {
this.authState = authState;
}
console.log(authState);
}, (error) => {
throw new Error(error.message);
});
}
public get currentUid(): string {
return this.authState ? this.authState.uid : undefined;
}
public get currentUser(): firebase.User {
return this.authState ? this.authState : undefined;
}
public get currentUserObservable(): Observable<firebase.User> {
return this.afAuth.authState;
}
public get isAnonymous(): boolean {
return this.authState ? this.authState.isAnonymous : false;
}
public get isAuthenticated(): boolean {
return !!this.authState;
}
public logout(): void {
this.afAuth.auth.signOut();
}
}
auth.service.spec.ts
import { async, fakeAsync, inject, TestBed, tick } from '@angular/core/testing';
import { AngularFireModule } from 'angularfire2';
import { AngularFireAuth, AngularFireAuthModule } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import 'rxjs/add/observable/of';
// import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Rx';
import { AuthService } from './auth.service';
import { environment } from '../../environments/environment';
const authState = {
isAnonymous: true,
uid: '17WvU2Vj58SnTz8v7EqyYYb0WRc2'
} as firebase.User;
describe('AuthService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AngularFireModule.initializeApp(environment.firebaseAppConfig)],
providers: [
AngularFireAuth,
AuthService
]
});
});
it('should be defined', inject([ AuthService ], (service: AuthService) => {
expect(service).toBeDefined();
}));
it('.currentUser should be anonymous', inject([ AuthService ], (service: AuthService) => {
Reflect.set(service, 'authState', authState);
expect(service.currentUser).toBe(authState);
}));
it('.currentUser should be undefined', inject([ AuthService ], (service: AuthService) => {
expect(service.currentUser).toBe(undefined);
}));
it('.currentUserObservable should be anonymous', inject([ AuthService ], (service: AuthService) => {
Reflect.set(service, 'authState', authState);
service.currentUserObservable.subscribe((value) => {
expect(value).toBe(authState);
});
}));
it('.currentUserObservable should be undefined', inject([ AuthService ], (service: AuthService) => {
service.currentUserObservable.subscribe((value) => {
expect(value).toBe(undefined);
});
}));
it('.currentUid should be of type String', inject([ AuthService ], (service: AuthService) => {
Reflect.set(service, 'authState', authState);
expect(service.currentUid).toBe(authState.uid);
}));
it('.currentUid should be undefined', inject([ AuthService ], (service: AuthService) => {
expect(service.currentUid).toBe(undefined);
}));
it('.isAnonymous should be false', inject([ AuthService ], (service: AuthService) => {
expect(service.isAnonymous).toBe(false);
}));
it('.isAnonymous should be true', inject([ AuthService ], (service: AuthService) => {
Reflect.set(service, 'authState', authState);
expect(service.isAnonymous).toBe(true);
}));
});
对于奖励积分,两个排除的测试(.currentUserObservable should be anonymous
和 .currentUserObservable should be undefined
)抛出错误 Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out
但只有当我在 authService
的实例化期间登录到控制台时.我想知道为什么会这样?
我需要创建并监视 mockAngularFireAuth
的 authState
和 return 一个 Observable,我可以在 onSuccess
或 [=14] 中订阅和期望它=] 函数,la:
import { TestBed, async, inject } from '@angular/core/testing';
import { AngularFireAuth } from 'angularfire2/auth';
import 'rxjs/add/observable/of';
import { Observable } from 'rxjs/Rx';
import { AuthService } from './auth.service';
import { MockUser} from './mock-user';
import { environment } from '../environments/environment';
describe('AuthService', () => {
// An anonymous user
const authState: MockUser = {
displayName: null,
isAnonymous: true,
uid: '17WvU2Vj58SnTz8v7EqyYYb0WRc2'
};
const mockAngularFireAuth: any = {
auth: jasmine.createSpyObj('auth', {
'signInAnonymously': Promise.reject({
code: 'auth/operation-not-allowed'
}),
// 'signInWithPopup': Promise.reject(),
// 'signOut': Promise.reject()
}),
authState: Observable.of(authState)
};
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: AngularFireAuth, useValue: mockAngularFireAuth },
{ provide: AuthService, useClass: AuthService }
]
});
});
it('should be created', inject([ AuthService ], (service: AuthService) => {
expect(service).toBeTruthy();
}));
…
describe('catastrophically fails', () => {
beforeEach(() => {
const spy = spyOn(mockAngularFireAuth, 'authState');
spy.and.returnValue(Observable.throw(new Error('Catastrophe')));
});
describe('AngularFireAuth.authState', () => {
it('should invoke it’s onError function', () => {
mockAngularFireAuth.authState.subscribe(null,
(error: Error) => {
expect(error).toEqual(new Error('Catastrophe'));
});
});
});
});
…
});
我有一个 authService
实例化时订阅 AngularFireAuth
's Observable authState
and sets the services' 内部(私有)属性 authState
.
所以我可以进行单元测试 authService
我在我的测试规范中用 Reflect.get/set
劫持了服务的内部 authState
这样我就可以控制它的值。
问题当然是 authService
在其实例化期间仍然订阅 AngularFireAuth
的 Observable authState
而我没有想要,也不需要。
我 假设 我需要模拟 AngularFireAuth,它伪造订阅并且实际上不与 Firebase 通信?单元测试的新手,我不知道该怎么做。
auth.service.ts
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class AuthService {
private authState: firebase.User;
constructor(private afAuth: AngularFireAuth) { this.init(); }
private init(): void {
this.afAuth.authState.subscribe((authState) => {
if (authState === null) {
this.afAuth.auth.signInAnonymously()
.then((authState) => {
this.authState = authState;
})
.catch((error) => {
throw new Error(error.message);
});
} else {
this.authState = authState;
}
console.log(authState);
}, (error) => {
throw new Error(error.message);
});
}
public get currentUid(): string {
return this.authState ? this.authState.uid : undefined;
}
public get currentUser(): firebase.User {
return this.authState ? this.authState : undefined;
}
public get currentUserObservable(): Observable<firebase.User> {
return this.afAuth.authState;
}
public get isAnonymous(): boolean {
return this.authState ? this.authState.isAnonymous : false;
}
public get isAuthenticated(): boolean {
return !!this.authState;
}
public logout(): void {
this.afAuth.auth.signOut();
}
}
auth.service.spec.ts
import { async, fakeAsync, inject, TestBed, tick } from '@angular/core/testing';
import { AngularFireModule } from 'angularfire2';
import { AngularFireAuth, AngularFireAuthModule } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import 'rxjs/add/observable/of';
// import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Rx';
import { AuthService } from './auth.service';
import { environment } from '../../environments/environment';
const authState = {
isAnonymous: true,
uid: '17WvU2Vj58SnTz8v7EqyYYb0WRc2'
} as firebase.User;
describe('AuthService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AngularFireModule.initializeApp(environment.firebaseAppConfig)],
providers: [
AngularFireAuth,
AuthService
]
});
});
it('should be defined', inject([ AuthService ], (service: AuthService) => {
expect(service).toBeDefined();
}));
it('.currentUser should be anonymous', inject([ AuthService ], (service: AuthService) => {
Reflect.set(service, 'authState', authState);
expect(service.currentUser).toBe(authState);
}));
it('.currentUser should be undefined', inject([ AuthService ], (service: AuthService) => {
expect(service.currentUser).toBe(undefined);
}));
it('.currentUserObservable should be anonymous', inject([ AuthService ], (service: AuthService) => {
Reflect.set(service, 'authState', authState);
service.currentUserObservable.subscribe((value) => {
expect(value).toBe(authState);
});
}));
it('.currentUserObservable should be undefined', inject([ AuthService ], (service: AuthService) => {
service.currentUserObservable.subscribe((value) => {
expect(value).toBe(undefined);
});
}));
it('.currentUid should be of type String', inject([ AuthService ], (service: AuthService) => {
Reflect.set(service, 'authState', authState);
expect(service.currentUid).toBe(authState.uid);
}));
it('.currentUid should be undefined', inject([ AuthService ], (service: AuthService) => {
expect(service.currentUid).toBe(undefined);
}));
it('.isAnonymous should be false', inject([ AuthService ], (service: AuthService) => {
expect(service.isAnonymous).toBe(false);
}));
it('.isAnonymous should be true', inject([ AuthService ], (service: AuthService) => {
Reflect.set(service, 'authState', authState);
expect(service.isAnonymous).toBe(true);
}));
});
对于奖励积分,两个排除的测试(.currentUserObservable should be anonymous
和 .currentUserObservable should be undefined
)抛出错误 Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out
但只有当我在 authService
的实例化期间登录到控制台时.我想知道为什么会这样?
我需要创建并监视 mockAngularFireAuth
的 authState
和 return 一个 Observable,我可以在 onSuccess
或 [=14] 中订阅和期望它=] 函数,la:
import { TestBed, async, inject } from '@angular/core/testing';
import { AngularFireAuth } from 'angularfire2/auth';
import 'rxjs/add/observable/of';
import { Observable } from 'rxjs/Rx';
import { AuthService } from './auth.service';
import { MockUser} from './mock-user';
import { environment } from '../environments/environment';
describe('AuthService', () => {
// An anonymous user
const authState: MockUser = {
displayName: null,
isAnonymous: true,
uid: '17WvU2Vj58SnTz8v7EqyYYb0WRc2'
};
const mockAngularFireAuth: any = {
auth: jasmine.createSpyObj('auth', {
'signInAnonymously': Promise.reject({
code: 'auth/operation-not-allowed'
}),
// 'signInWithPopup': Promise.reject(),
// 'signOut': Promise.reject()
}),
authState: Observable.of(authState)
};
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: AngularFireAuth, useValue: mockAngularFireAuth },
{ provide: AuthService, useClass: AuthService }
]
});
});
it('should be created', inject([ AuthService ], (service: AuthService) => {
expect(service).toBeTruthy();
}));
…
describe('catastrophically fails', () => {
beforeEach(() => {
const spy = spyOn(mockAngularFireAuth, 'authState');
spy.and.returnValue(Observable.throw(new Error('Catastrophe')));
});
describe('AngularFireAuth.authState', () => {
it('should invoke it’s onError function', () => {
mockAngularFireAuth.authState.subscribe(null,
(error: Error) => {
expect(error).toEqual(new Error('Catastrophe'));
});
});
});
});
…
});