2017-09-16 134 views
-1

在我使用AngularFire2的Angular2應用中,我有一個AuthService,它嘗試使用Firebase匿名進行身份驗證。無法訂閱Jasmine Observable的測試

我正在嘗試編寫一個測試,期望訂閱AngularFireAuthauthState失敗(可觀察序列的異常終止)以及要拋出的錯誤。

我問了一個看起來是similar question的東西,然而,在這裏我正在測試「可觀察序列的異常終止」 - 一個災難性故障,例如,第三方提供商Firebase關閉時。

這是我的另一個(相關)問題,我正在測試被拒絕的承諾。

下面是一個簡化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(); 
    })); 
    }); 

    // 
    // 
    // 
    // 
    // 

}); 

我不知道這甚至有可能,或需要 - 因爲這將是一個非常特殊的情況。如果我要開始測試,儘管我希望測試儘可能全面和防水!感謝您的幫助!

+0

[測試與茉莉花拒絕承諾(可能的重複https://stackoverflow.com/questions/46252850/test-拒絕承諾與茉莉花) – jonrsharpe

+0

謝謝@jonrsharpe但這不是一個騙局;這個Q詢問如何編寫一個測試規範,當試圖訂閱AngularFireAuth的'authState'時,如果有'可觀察序列的異常終止',那麼這個錯誤就會被拋出。乾杯 –

+0

@jonrsharpe你投我的兩個問題了?他們不是重複的,我覺得他們清楚地解釋了這些問題;期望的結果是什麼,並有簡明的代碼示例。你能建議我如何改進這些問題嗎?你能幫忙嗎? –

回答

0

我需要監視mockAngularFireAuthauthState並返回一個拋出錯誤的Observable。當我在onError功能訂閱mockAngularFireAuth.authState我應該期待的錯誤,一拉:

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