在角度测试中注入位置

时间:2018-03-13 16:20:13

标签: angular testing karma-runner

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

  

错误:StaticInjectorError(DynamicTestModule)[CreateGroupComponent - >   Location]:StaticInjectorError(Platform:core)[CreateGroupComponent    - >地点]:       NullInjectorError:没有Location的提供者!

这是我的测试类

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

1 个答案:

答案 0 :(得分:2)

我承认,我还在学习Angular测试,但是......你不需要添加&#34; RouterTestingModule&#34;到你的TestBed&#34;进口:[]&#34;阵列

imports: [RouterTestingModule, ReactiveFormsModule],