在角度测试中注入位置

Dap*_*rio 2 testing karma-runner angular

我在角度组件中使用了位置注入功能,以将用户重定向到另一条路线。它按预期工作,但是当我运行测试时,我收到此消息

错误:StaticInjectorError(DynamicTestModule)[CreateGroupComponent->位置]:StaticInjectorError(平台:核心)[CreateGroupComponent->位置]:NullInjectorError:没有位置提供者!

这是我的测试课

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { CreateGroupComponent } from './create-group.component';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { GroupService } from '../core/groupService/group.service';
import { AuthService } from '../core/authService/auth.service';
import { RouterTestingModule } from '@angular/router/testing';

describe('CreateGroupComponent', () => {
  let component: CreateGroupComponent;
  let fixture: ComponentFixture<CreateGroupComponent>;

  beforeEach(async(() => {

    const mockGroupService: any = {

    };

    const mockAuthService: any = {

    };

    TestBed.configureTestingModule({
      imports: [ReactiveFormsModule],
      declarations: [CreateGroupComponent],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
      providers: [{ provide: AuthService, useValue: mockAuthService }, { provide: GroupService, useValue: mockGroupService }]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(CreateGroupComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});
Run Code Online (Sandbox Code Playgroud)

Ric*_*ick 5

我承认,我也仍在学习Angular测试,但是...您是否不需要在TestBed的“ imports:[]”数组中添加“ RouterTestingModule”?

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