Angular Testing中没有$ injector的提供者

问题描述 投票:2回答:1

我一直在尝试在我们的Hybrid AngularJS / NG6应用程序中设置测试,但是这很难做到。我不断得到错误。最新的是以下内容:

错误:StaticInjectorError(DynamicTestModule)[$ injector]:

StaticInjectorError(平台:核心)[$ injector]:

NullInjectorError:没有$ injector的提供者!

我有以下component

import { Component, OnInit, Input, Inject } from '@angular/core';
import { DashboardService } from '../../services/dashboard/dashboard.service';

@Component({
    templateUrl: './views/components/dashboard/dashboard.component.html'
})
export class DashboardComponent implements OnInit {
    @Input()
    Session;
    Util;
    constructor(
        private _dashboardService: DashboardService,
        @Inject('Session') Session: any,
        @Inject('Util') Util: any
    ) {
        this.Session = Session;
        this.Util = Util;
    }

    ngOnInit() {
        this._dashboardService
            .getPrograms(this.Session.user.organization)
            .subscribe(
                data => {
                    console.log(data);
                },
                error => {
                    console.log(error);
                }
            );
    }
}

这完全没问题。我可以从我们的API中提取数据。另一方面,我有这个spec文件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';

import { DashboardComponent } from './dashboard.component';
import { DebugElement } from '@angular/core';

import { DashboardService } from '../../services/dashboard/dashboard.service';
import { ApiService } from '../../services/api.service';

import { HttpClientModule } from '@angular/common/http';

describe('The Dashboard', () => {
    let component: DashboardComponent;
    let fixture: ComponentFixture<DashboardComponent>;
    let de: DebugElement;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                CommonModule,
                FormsModule,
                ReactiveFormsModule,
                HttpClientModule
            ],
            declarations: [DashboardComponent],
            providers: [
                {
                    provide: 'Util',
                    useFactory: ($injector: any) => $injector.get('Util'),
                    deps: ['$injector']
                },
                {
                    provide: 'Session',
                    useFactory: ($injector: any) => $injector.get('Session'),
                    deps: ['$injector']
                },
                DashboardService,
                ApiService            
            ]
        })
            .overrideComponent(DashboardComponent, {
                set: {
                    templateUrl:
                        '/dist/views/components/dashboard/dashboard.component.html'
                }
            })
            .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(DashboardComponent);
        component = fixture.componentInstance;
        de = fixture.debugElement;
        fixture.detectChanges();
    });

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

当我运行这个spec文件时,我得到上面列出的错误。我不知道这个错误试图告诉我什么,因为它很模糊。

如何正确地将$Injector模块提供到spec文件中?

angular karma-jasmine angular-test angular-testing angular-hybrid
1个回答
2
投票

我还尝试在我的混合应用程序中测试Angular部件与AngularJS的依赖关系,但我没有成功。要测试具有Angular依赖关系的AngularJS部件或具有AngularJS依赖关系的Angular部件是非常困难的。 我在GitHub上找到了这个post的两个可能的解决方案 - 完全模拟其他框架中的部件。 - 创建包含来自其他框架的所有依赖项的迷你应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.