Angular 7 测试:NullInjectorError:没有 ActivatedRoute 的提供者

新手上路,请多包涵

您好,在测试我使用 Angular 7 制作的应用程序时出现一些错误。我在角度方面没有太多经验,所以我需要您的帮助+

 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();
    }
  )
)
});

我真的找不到任何解决方案。

丹妮尔

原文由 Daniele Caputo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 690
2 个回答

添加以下导入

  imports: [
    RouterModule.forRoot([]),
    ...
  ],

原文由 Sachila Ranawaka 发布,翻译遵循 CC BY-SA 4.0 许可协议

您必须导入 RouterTestingModule

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

imports: [
    ...
    RouterTestingModule
    ...
]

原文由 RaulDanielPopa 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题