Reputation: 165
I user Location injection in my angular component to redirect the user to another route. It works as intended but when I run the tests I get this message
Error: StaticInjectorError(DynamicTestModule)[CreateGroupComponent -> Location]: StaticInjectorError(Platform: core)[CreateGroupComponent -> Location]: NullInjectorError: No provider for Location!
Here is my Test class
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();
});
});
Upvotes: 2
Views: 2381
Reputation: 255
I admit, I'm still learning Angular testing too, but... Don't you need to add "RouterTestingModule" to your TestBed's "imports:[]" array?
imports: [RouterTestingModule, ReactiveFormsModule],
Upvotes: 7