Angular 7 Test: NullInjectorError: No provider for ActivatedRoute
Angular 7 Test: NullInjectorError: No provider for ActivatedRoute
您好,在测试我使用 Angular 7 制作的应用程序时遇到一些错误。我在 angular 方面没有太多经验,所以我需要您的帮助+
Error: StaticInjectorError(DynamicTestModule)[BeerDetailsComponent -> ActivatedRoute]:
StaticInjectorError(Platform: core)[BeerDetailsComponent -> ActivatedRoute]:
NullInjectorError: No provider for ActivatedRoute!
测试代码是这样的:
import { async, ComponentFixture, TestBed, inject } from '@angular/core/testing';
import { BeerDetailsComponent } from './beer-details.component';
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing';
describe('BeerDetailsComponent', () => {
let component: BeerDetailsComponent;
let fixture: ComponentFixture<BeerDetailsComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
declarations: [ BeerDetailsComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BeerDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create',
inject(
[HttpTestingController],
() => {
expect(component).toBeTruthy();
}
)
)
});
实在找不到解决方法
丹妮尔
添加以下导入
imports: [
RouterModule.forRoot([]),
...
],
您必须导入 RouterTestingModule
import { RouterTestingModule } from "@angular/router/testing";
imports: [
...
RouterTestingModule
...
]
使用服务和 ActivatedRoute 的简单测试示例!祝你好运!
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientModule } from '@angular/common/http';
import { MyComponent } from './my.component';
import { ActivatedRoute } from '@angular/router';
import { MyService } from '../../core/services/my.service';
describe('MyComponent class test', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
let teste: MyComponent;
let route: ActivatedRoute;
let myService: MyService;
beforeEach(async(() => {
teste = new MyComponent(route, myService);
TestBed.configureTestingModule({
declarations: [ MyComponent ],
imports: [
RouterTestingModule,
HttpClientModule
],
providers: [MyService]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('Checks if the class was created', () => {
expect(component).toBeTruthy();
});
});
我在测试时也遇到过这个错误,none 这些答案对我很有帮助。为我修复它的是同时导入 RouterTestingModule 和 HttpClientTestingModule。
所以基本上它在你的 whatever.component.spec.ts 文件中看起来像这样。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ProductComponent],
imports: [RouterTestingModule, HttpClientTestingModule],
}).compileComponents();
}));
您可以从
获取进口商品
import { RouterTestingModule } from "@angular/router/testing";
import { HttpClientTestingModule } from "@angular/common/http/testing";
我不知道这是否是最佳解决方案,但这对我有用。
此问题可以按如下方式解决:
在你对应的spec.ts文件中导入RouterTestingModule
import { RouterTestingModule } from '@angular/router/testing';
在同一文件中添加 RouterTestingModule 作为导入之一
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [Service]
});
});
您好,在测试我使用 Angular 7 制作的应用程序时遇到一些错误。我在 angular 方面没有太多经验,所以我需要您的帮助+
Error: StaticInjectorError(DynamicTestModule)[BeerDetailsComponent -> ActivatedRoute]:
StaticInjectorError(Platform: core)[BeerDetailsComponent -> ActivatedRoute]:
NullInjectorError: No provider for ActivatedRoute!
测试代码是这样的:
import { async, ComponentFixture, TestBed, inject } from '@angular/core/testing';
import { BeerDetailsComponent } from './beer-details.component';
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing';
describe('BeerDetailsComponent', () => {
let component: BeerDetailsComponent;
let fixture: ComponentFixture<BeerDetailsComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
declarations: [ BeerDetailsComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BeerDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create',
inject(
[HttpTestingController],
() => {
expect(component).toBeTruthy();
}
)
)
});
实在找不到解决方法
丹妮尔
添加以下导入
imports: [
RouterModule.forRoot([]),
...
],
您必须导入 RouterTestingModule
import { RouterTestingModule } from "@angular/router/testing";
imports: [
...
RouterTestingModule
...
]
使用服务和 ActivatedRoute 的简单测试示例!祝你好运!
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientModule } from '@angular/common/http';
import { MyComponent } from './my.component';
import { ActivatedRoute } from '@angular/router';
import { MyService } from '../../core/services/my.service';
describe('MyComponent class test', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
let teste: MyComponent;
let route: ActivatedRoute;
let myService: MyService;
beforeEach(async(() => {
teste = new MyComponent(route, myService);
TestBed.configureTestingModule({
declarations: [ MyComponent ],
imports: [
RouterTestingModule,
HttpClientModule
],
providers: [MyService]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('Checks if the class was created', () => {
expect(component).toBeTruthy();
});
});
我在测试时也遇到过这个错误,none 这些答案对我很有帮助。为我修复它的是同时导入 RouterTestingModule 和 HttpClientTestingModule。
所以基本上它在你的 whatever.component.spec.ts 文件中看起来像这样。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ProductComponent],
imports: [RouterTestingModule, HttpClientTestingModule],
}).compileComponents();
}));
您可以从
获取进口商品import { RouterTestingModule } from "@angular/router/testing";
import { HttpClientTestingModule } from "@angular/common/http/testing";
我不知道这是否是最佳解决方案,但这对我有用。
此问题可以按如下方式解决:
在你对应的spec.ts文件中导入RouterTestingModule
import { RouterTestingModule } from '@angular/router/testing';
在同一文件中添加 RouterTestingModule 作为导入之一
beforeEach(() => { TestBed.configureTestingModule({ imports: [RouterTestingModule], providers: [Service] }); });