Angular2 Karma测试失败:没有Token FirebaseUrl的提供者

时间:2017-03-09 10:31:15

标签: angular firebase karma-jasmine angularfire2

我使用angular-cli来生成我的项目。这是我的测试由angular-cli生成并更新为包括router,angularmaterial2和angularfire2

import { async, TestBed } from '@angular/core/testing';
import { MaterialModule } from '@angular/material';
import { RouterModule } from '@angular/router';
import { AngularFireModule, AuthMethods, AuthProviders } from 'angularfire2';
import { } from 'jasmine';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        MaterialModule.forRoot(),
        RouterModule,
        AngularFireModule,
      ],
      declarations: [
        AppComponent,
      ],
      providers: [
      ],
    }).compileComponents();
  }));

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
});

当我运行它时,我得到DI错误:失败:没有Token FirebaseUrl的提供者! 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

配置AngularFireModule解决了这个问题。

  beforeEach(async(() => {
const firebaseConfig = {
  apiKey: 'xxx',
  authDomain: 'xxx',
  databaseURL: 'xxx',
  storageBucket: 'xxx',
  messagingSenderId: 'xxx',
};

TestBed.configureTestingModule({
  imports: [
    MaterialModule.forRoot(),
    RouterModule,
    AngularFireModule.initializeApp(firebaseConfig),
  ],
  declarations: [
    AppComponent,
  ],
  providers: [
  ],
}).compileComponents();

}));