2016-06-26 15 views
12

Próbuję skonfigurować testy jednostkowe dla próbki Kątowymi 2 aplikacji przy użyciu AngularFire 2 auth, składnik ten jest dość prosty:Jak wyśmiewać usługę AngularFire 2 w teście jednostkowym?

import { Component } from '@angular/core'; 
import { AngularFire, AuthProviders } from 'angularfire2'; 

@Component({ 
    moduleId: module.id, 
    selector: 'app-root', 
    templateUrl: 'app.component.html', 
    styleUrls: ['app.component.css'] 
}) 
export class AppComponent { 
    isLoggedIn: boolean; 

    constructor(public af: AngularFire) { 
    this.af.auth.subscribe(auth => { 
     if (auth) { 
     this.isLoggedIn = true; 
     } else { 
     this.isLoggedIn = false; 
     } 
    }); 
    } 

    loginWithFacebook() { 
    this.af.auth.login({ 
     provider: AuthProviders.Facebook 
    }); 
    } 

    logout() { 
    this.af.auth.logout(); 
    } 
} 

Wszystko robię jest owijanie wokół login i logout metod AngularFire tak myślałem o użyciu makiety, aby sprawdzić, czy metody były nazywane, ale nie jestem pewien, gdzie zacząć, próbowałem robić następujących w moim pliku spec:

import { provide } from '@angular/core'; 
import { AngularFire } from 'angularfire2'; 
import { 
    beforeEach, beforeEachProviders, 
    describe, xdescribe, 
    expect, it, xit, 
    async, inject 
} from '@angular/core/testing'; 
import { AppComponent } from './app.component'; 

spyOn(AngularFire, 'auth'); 

beforeEachProviders(() => [ 
    AppComponent, 
    AngularFire 
]); 

describe('App Component',() => { 
    it('should create the app', 
    inject([AppComponent], (app: AppComponent) => { 
     expect(app).toBeTruthy(); 
    }) 
); 

    it('should log user in', 
    inject([AppComponent], (app: AppComponent) => { 
     expect(app.fb.auth.login).toHaveBeenCalled(); 
    }) 
); 

    it('should log user out', 
    inject([AppComponent], (app: AppComponent) => { 
     expect(app.fb.auth.logout).toHaveBeenCalled(); 
    }) 
); 
}); 

jednak nie jestem pewien, w jaki sposób drwić z metod login i logout, ponieważ są one częścią Właściwość auth, czy istnieje sposób, aby kpić auth, a także powracających metod login i logout?

+3

Zainteresowany czytelnik powinien śledzić [ten problem] (https://github.com/angular/angularfire2/issues/18) dotyczący zmniejszenia tego obciążenia. – drewmoore

Odpowiedz

13

W tym fragmencie:

beforeEach(() => addProviders([ 
    AppComponent, 
    AngularFire 
]); 

ustawić (lub override) dostawcy, które zostaną wykorzystane w teście.

W ten sposób można utworzyć inną klasę, udając, że to zrobisz, i używając notacji { provide: originalClass, useClass: fakeClass }, podaj ją zamiast rzeczywistej klasy AngularFire.

coś takiego:

class AngularFireAuthMock extends AngularFireAuth {   // added this class 
    public login() { ... } 
    public logout() { ... } 
} 

class AngularFireMock extends AngularFire {     // added this class 
    public auth: AngularFireAuthMock; 
} 

beforeEach(() => addProviders([ 
    AppComponent, 
    { provide: AngularFire, useClass: AngularFireMock }   // changed this line 
]); 

A AngularFire w twoich testów będą AngularFireMock s.

Powiązane problemy