Mocking firebase import for jasmine/karma in ionic 3/angular 5

Viewed 357

I'm new to setting up tests for ionic 3/angular 5 services using karma & jasmine. currently i am trying to mock the firebase library in a simple test for a service.

As i have undestood from reading the angular docs testBed is not needed for testing services, or am i wrong in this case?

Currently i am trying to firebase for a service, but i keep getting the error: [DEFAULT]: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).

I have been trying for a few hours now, my best guess so far is this:

apiService.spec.ts

import { ApiServiceProvider } from '../api-service/api-service'
import * as firebase from 'firebase'

describe('FancyService without the TestBed', () => {
    let apiService: ApiServiceProvider
    let firebase: any

    beforeEach(() => {
        const firebase = {
            child(){},
            database(){
                return {
                    ref(){}
                }
            }
        }
    })

    // When someMethod is called, the error occurs.
    it('run some method', () => {
        const result = apiService.someMethod(....)
        expect(distance).toBe(100)
    })
})

in the apiService.ts file firebase is setup like this. As for now all it the service does with firebase is to bind it to a variable:

import * as firebase from 'firebase'

@Injectable()
export class ApiServiceProvider {
    private _ref = firebase.database().ref()

    constructor(....) {}
}
0 Answers
Related