2017-03-16 100 views
0

創造了角CLI與Angularfire2單元測試的角度,CLI

下面以一個新的角度2項目是默認組件app.component.ts,它有app.component.spec.ts

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

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    title = 'app works!'; 
    af: AngularFire; 
    constructor(af: AngularFire){ 
    this.af=af; 
    this.firebaseCall(); 
    } 
    //push data to firebase collection 
    firebaseCall(){ 
    let post=this.af.database.list('/post'); 
    post.push({a:'test'}); 
    } 

} 

貫徹單元測試用於上述firebaseCall()在app.component.spec.ts

我已添加/在更新app.component.spec.ts下面線

import { AngularFire } from 'angularfire2'; 
beforeEach(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ 
     AppComponent,AngularFire // extra added 
     ], 

    }); 
    TestBed.compileComponents(); 

    }); 

我得到以下錯誤,而納克測試

預期的值「AngularFire」被模塊DynamicTestModule'

+0

查看AngularFire2源代碼,看看如何配置TestBed:https://github.com/angular/angularfire2/blob/2.0.0-beta.8/src/angularfire2.spec.ts #L31-L52 – cartant

+0

添加了此行導入:[AngularFireModule.initializeApp(COMMON_CONFIG)] 獲取此錯誤 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超時內沒有調用異步回調。 – Prithvi

回答

0

您需要在測試嘲笑服務聲明。 這裏您注射AngularFire。所以你的測試設置應該是這樣的。

import { AngularFire } from 'angularfire2'; 
... 
    const mockFirebase = jasmine.createSpyObj('af',['database']); 
    af.database.and.returnValue({list: Rx.Observable.of([])}); 
    beforeEach(() => { 
     TestBed.configureTestingModule({ 
      declarations: [ 
      AppComponent 
      ], 
      providers:[ 
      {provide:AngularFire, useValue:}] 
     }); 
     TestBed.compileComponents(); 

     }); 

希望,這可以幫助你。總是在你的單元測試中嘲笑你的提供者,並且不要發起http調用。