Angular 2 - Error: No provider for Token FirebaseA

2019-07-19 16:14发布

问题:

My app is running fine, but I cannot get my tests to pass with Firebase.

Error: No provider for Token FirebaseAppName!

Here is me spec:

import { TestBed, inject } from '@angular/core/testing';
import { ApiService } from './api.service';
import { HttpModule } from '@angular/http';
import {FIREBASE_PROVIDERS, defaultFirebase, AngularFire} from 'angularfire2';

describe('ApiService', () => {
  beforeEach(() => {
    const firebaseConfig = {
      apiKey: "foo",
      authDomain: "bar",
      databaseURL: "baz",
      storageBucket: "foo",
      messagingSenderId: "bar"
    }

    TestBed.configureTestingModule({
      // providers: [ApiService, defaultFirebase(firebaseConfig)],
     providers: [ApiService, FIREBASE_PROVIDERS, defaultFirebase(firebaseConfig)],
     imports: [
       HttpModule
     ]
   });
 });

 it('should ...', inject([ApiService], (service: ApiService) => {
   expect(service).toBeTruthy();
 }));
});

Thanks in advance.

Versions:

"@angular/core": "^2.4.0",
"jasmine-core": "~2.5.2",
"firebase": "^3.7.0",

回答1:

Try to configure module instead of provider

TestBed.configureTestingModule({
  providers: [ApiService]
  imports: [
    HttpModule,
    AngularFireModule.initializeApp(firebaseConfig)
  ]
});