2017-03-16 101 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调用。