首页 文章

在角度测试中注入位置

提问于
浏览
0

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

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

这是我的Test课程

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

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

    imports: [RouterTestingModule, ReactiveFormsModule],
    

相关问题