6

我一直在尝试在我们的 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文件中?

4

1 回答 1

4

我还尝试在我的混合应用程序中测试依赖于 AngularJS 的 Angular 部件,但我没有成功。在混合中测试具有 Angular 依赖项的 AngularJS 部件或具有 AngularJS 依赖项的 Angular 部件是非常困难的。 我从GitHub 上的这篇文章

中 找到了两种可能的解决方案 - 完全模拟来自其他框架的部分。 - 创建包含来自其他框架的所有依赖项的迷你应用程序。

于 2019-02-12T19:04:55.313 回答