在使用AngularFire2我Angular2的应用程序,我有尝试与火力地堡匿名身份验证的AuthService
。
我试图写一个测试,预计订阅的AngularFireAuth
的authState
失败(可观察序列的一个特殊的终止)和一个错误被抛出。
我曾问一个似乎是一个similar question,但是,我在这里测试为“可观察序列的特殊终止” - 例如灾难性故障当第三方提供商火力地堡已关闭。
这是单独的地方,我的拒绝承诺测试我的其他(相关)的问题。
这里是一个简化的AuthService
:
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: firebase.User) => {
if (authState === null) {
this.afAuth.auth.signInAnonymously()
.then((authState) => {
this.authState = authState;
})
.catch((error) => {
throw new Error(error.message);
});
} else {
this.authState = authState;
}
}, (error) => {
throw new Error(error.message);
});
}
}
这里是我的测试规格:
import { TestBed, 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 { environment } from '../environments/environment';
describe('AuthService', () => {
const mockAngularFireAuth: any = {
auth: jasmine.createSpyObj('auth', {
'signInAnonymously': Promise.resolve('foo'),
// 'signInWithPopup': Promise.reject(),
// 'signOut': Promise.reject()
}),
authState: Observable.of(null)
};
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: AngularFireAuth, useValue: mockAngularFireAuth },
{ provide: AuthService, useClass: AuthService }
]
});
});
it('should be created', inject([ AuthService ], (service: AuthService) => {
expect(service).toBeTruthy();
}));
//
//
//
//
//
describe('when we can’t authenticate', () => {
beforeEach(() => {
mockAngularFireAuth.auth.signInAnonymously.and.returnValue(Promise.reject('bar'));
});
it('should thow', inject([ AuthService ], (service: AuthService) => {
expect(service).toThrow();
}));
});
//
//
//
//
//
});
我不知道这甚至有可能,或需要 - 因为这将是一个非常特殊的情况。如果我要开始测试,虽然我想测试是全面的和水密越好,但!谢谢您的帮助!
我需要刺探mockAngularFireAuth
的authState
并返回一个可观察抛出一个错误。当我订阅的mockAngularFireAuth.authState
功能onError
我应该期待的错误,一拉:
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'));
});
});
});
});
…
});