unitarios unitarias unit test run pruebas angular jasmine karma-jasmine angularfire2

unitarias - unit test angular 4



Prueba de promesa rechazada con Jasmine (1)

En mi aplicación Angular2 que usa AngularFire2, tengo un AuthService que intenta autenticarse de forma anónima con Firebase.

Estoy intentando escribir una prueba que espera que AngularFireAuth ''s signInAnonymously devuelva una promesa rechazada; para que authState sea null y se authState un error.

Soy nuevo en Jasmine y las pruebas en general, pero creo que es posible que necesite utilizar pruebas asincrónicas, pero me estoy quedando bastante estancado.

Aquí hay un AuthService simplificado:

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); }); } }

Y aquí están mis especificaciones de prueba:

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(mockAngularFireAuth.auth.signInAnonymously).toThrow(); })); }); // // // // // });

¡Gracias por tu ayuda!


Resulta que me estaba burlando de mockAngularFireAuth correctamente. Necesitaba rechazar la mockAngularFireAuth.auth signInAnonymously() con un error y esperar que sea atrapada, a 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(''can authenticate anonymously'', () => { describe(''AngularFireAuth.auth.signInAnonymously()'', () => { it(''should return a resolved promise'', () => { mockAngularFireAuth.auth.signInAnonymously() .then((data: MockUser) => { expect(data).toEqual(authState); }); }); }); }); describe(''can’t authenticate anonymously'', () => { describe(''AngularFireAuth.auth.signInAnonymously()'', () => { it(''should return a rejected promise'', () => { mockAngularFireAuth.auth.signInAnonymously() .catch((error: { code: string }) => { expect(error.code).toEqual(''auth/operation-not-allowed''); }); }); }); }); … });