Angular 测试中没有 $injector 的提供者
No provider for $injector in Angular Testing
我一直在尝试在我们的混合 AngularJS/NG6 应用程序中设置测试,但是哇,这很难做到。我不断收到错误。最新的如下:
Error: StaticInjectorError(DynamicTestModule)[$injector]:
StaticInjectorError(Platform: core)[$injector]:
NullInjectorError: No provider for $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 依赖于 AngularJS 的部分,但我没有成功。在混合中测试具有 Angular 依赖项的 AngularJS 部分或具有 AngularJS 依赖项的 Angular 部分是非常困难的。
我在 GitHub
上从这个 post 中找到了两个可能的解决方案
- 完全模拟来自其他框架的部分。
- 创建包含来自其他框架的所有依赖项的迷你应用程序。
我一直在尝试在我们的混合 AngularJS/NG6 应用程序中设置测试,但是哇,这很难做到。我不断收到错误。最新的如下:
Error: StaticInjectorError(DynamicTestModule)[$injector]:
StaticInjectorError(Platform: core)[$injector]:
NullInjectorError: No provider for $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 依赖于 AngularJS 的部分,但我没有成功。在混合中测试具有 Angular 依赖项的 AngularJS 部分或具有 AngularJS 依赖项的 Angular 部分是非常困难的。
我在 GitHub
上从这个 post 中找到了两个可能的解决方案
- 完全模拟来自其他框架的部分。
- 创建包含来自其他框架的所有依赖项的迷你应用程序。