我一直在尝试在我们的Hybrid AngularJS / NG6 App中设置测试,但是这很难做到。我一直在不断出错。最新的是以下内容:
错误: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
文件中?
答案 0 :(得分:2)
我还尝试在我的混合应用程序中使用对AngularJS的依赖关系来测试Angular零件,但是我没有成功。要在混合环境中测试具有Angular依赖关系的AngularJS零件或具有AngularJS依赖关系的Angular零件是非常困难的。
我在GitHub上的post中找到了两种可能的解决方案
-完全模拟其他框架中的部分。
-创建包含来自其他框架的所有依赖项的微型应用程序。