我在我的 Angular 组件中使用 Location 注入(inject)来将用户重定向到另一条路线。它按预期工作,但当我运行测试时,我收到此消息
Error: StaticInjectorError(DynamicTestModule)[CreateGroupComponent -> Location]: StaticInjectorError(Platform: core)[CreateGroupComponent -> Location]: NullInjectorError: No provider for 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();
});
});
最佳答案
我承认,我也在学习 Angular 测试,但是...您不需要将“RouterTestingModule”添加到您的 TestBed 的“imports:[]”数组吗?
imports: [RouterTestingModule, ReactiveFormsModule],
关于angular - 在 Angular Testing 中注入(inject)位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49261066/