关于angular:因AngularFireAuth而导致的业力测试失败

Karma test failing for AngularFireAuth

我已经使用AngularFire2为我的Angular2 Web应用程序使用jasmine / karma创建了测试用例,但遇到了一个我无法调试的奇怪错误。当我运行测试(检查应用程序组件是否存在)时,出现错误

Failed: app.auth is not a function
at new AngularFireAuth (http://localhost:9876/_karma_webpack_/webpack:/C:/path/to/project/node_modules/angularfire2/auth/auth.js:11:1)

查看AngularFire2文件本身,似乎正在尝试为其正在创建的当前应用程序分配新的身份验证系统,但是该代码对于我正在处理的其他Angular2项目也能正常工作。 AngularFire2的版本是4.0.0,Firebase的版本是4.4.0。


我在自动创建效果测试时遇到了同样的问题,并解决了创建Stubbing的问题:

1
2
3
const AngularFireMocks = {
    auth: Observable.of({ uid: 'ABC123' })
};

在我的测试提供程序中,我输入:

1
{ provide: AngularFireAuth, useValue: AngularFireMocks },

完整代码(是一种效果测试):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import {TestBed, inject} from '@angular/core/testing';
import {provideMockActions} from '@ngrx/effects/testing';
import {BehaviorSubject, Observable, of} from 'rxjs';

import {AuthEffects} from './auth.effects';
import {RouterModule} from '@angular/router';
import {RouterTestingModule} from '@angular/router/testing';
import {AngularFireAuth} from '@angular/fire/auth';

const AngularFireMocks = {
    auth: of({ uid: 'ABC123' })
};

describe('AuthEffects', () => {
    let actions$: Observable;
    let effects: AuthEffects;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [RouterTestingModule],
            providers: [
                AuthEffects,
                { provide: AngularFireAuth, useValue: AngularFireMocks },
                provideMockActions(() => actions$)
            ]
        });

        effects = TestBed.get(AuthEffects);
    });

    it('should be created', () => {
        expect(effects).toBeTruthy();
    });
});

我遇到了同样的问题,并通过从app.module.ts文件的提供者列表中删除" FirebaseApp "得以解决