If you are getting the following error while running ng test:
Failed: R3InjectorError(DynamicTestModule)[NavController -> UrlSerializer -> UrlSerializer]:
NullInjectorError: No provider for UrlSerializer!
the solution is to import RouterTestingModule in your spec file of your component
beforeEach(async () => {
await TestBed.configureTestingModule({
imports:[RouterTestingModule]
}).compileComponents();
The full file will look like this:
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { YourComponent } from './YourComponent.component';
describe('YourComponent', () => {
let component: YourComponent;
let fixture: ComponentFixture<YourComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports:[RouterTestingModule],
declarations:[....YourDeclarationsIfNeeded]
}).compileComponents();
fixture = TestBed.createComponent(YourComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});