Angular 7测试:NullInjectorError:ActivatedRoute没有提供程序

Dan*_*uto 5 testing jasmine karma-runner angular

您好,在使用Angular 7测试我的App时遇到一些错误。我在角度方面没有太多经验,所以我需要您的帮助+

Error: StaticInjectorError(DynamicTestModule)[BeerDetailsComponent -> ActivatedRoute]: 
  StaticInjectorError(Platform: core)[BeerDetailsComponent -> ActivatedRoute]: 
    NullInjectorError: No provider for ActivatedRoute!
Run Code Online (Sandbox Code Playgroud)

测试代码如下:

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();
    }
  )
)
});
Run Code Online (Sandbox Code Playgroud)

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

丹尼尔(Daniele)

小智 69

您必须导入 RouterTestingModule

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

imports: [
    ...
    RouterTestingModule
    ...
]
Run Code Online (Sandbox Code Playgroud)

  • 如果您在 2022 年遇到这种情况,那么“RouterTestingModule”现在位于“@angular/common/http/testing”中 (3认同)

小智 15

我在测试时也遇到了这个错误一段时间,但这些答案都没有真正帮助我。为我修复的是同时导入 RouterTestingModule 和 HttpClientTestingModule。

所以基本上它在你的whatever.component.spec.ts文件中看起来像这样。

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ProductComponent],
      imports: [RouterTestingModule, HttpClientTestingModule],
    }).compileComponents();
  }));
Run Code Online (Sandbox Code Playgroud)

您可以从

import { RouterTestingModule } from "@angular/router/testing";
import { HttpClientTestingModule } from "@angular/common/http/testing";
Run Code Online (Sandbox Code Playgroud)

我不知道这是否是最好的解决方案,但这对我有用。


Sac*_*aka 14

添加以下导入

  imports: [ 
    RouterModule.forRoot([]),
    ...
  ],
Run Code Online (Sandbox Code Playgroud)

  • RouterModule 不应包含在测试文件中 (6认同)
  • 这不是一个正确的解决方案,您需要定义所有 roter 模块提供程序参数、APP_BASE_HREF 令牌等。使用没有路由器提供程序声明的 RouterTestingModule (6认同)
  • 我应该在哪里导入 RouterModule.forRoot([]),? (2认同)
  • 在模块中,从'@ angular / router';导入import {RouterModule};` (2认同)

小智 8

此问题可以通过以下方式修复:

  1. 在您相应的spec.ts文件中导入 RouterTestingModule

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

  2. 在同一个文件中添加RouterTestingModule作为导入之一

     beforeEach(() => {
        TestBed.configureTestingModule({
        imports: [RouterTestingModule],
        providers: [Service]
      });
     });
    
    Run Code Online (Sandbox Code Playgroud)


小智 6

使用服务和 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();
    });

    });
Run Code Online (Sandbox Code Playgroud)