gpt4 book ai didi

javascript - Angular 7 测试 : NullInjectorError: No provider for ActivatedRoute

转载 作者:太空狗 更新时间:2023-10-29 18:03:44 26 4
gpt4 key购买 nike

您好,在测试我使用 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();
}
)
)
});

我真的找不到任何解决办法。

丹妮尔

最佳答案

你必须导入 RouterTestingModule

import { RouterTestingModule } from "@angular/router/testing";

imports: [
...
RouterTestingModule
...
]

关于javascript - Angular 7 测试 : NullInjectorError: No provider for ActivatedRoute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53654341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com