在Angular单元测试中,如果路由卡在根路径而不是重定向到指定的路径,可以尝试以下解决方法:
Location模拟路由重定向:import { Location } from '@angular/common';
import { RouterTestingModule } from '@angular/router/testing';
describe('YourComponent', () => {
  let location: Location;
  let router: Router;
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [RouterTestingModule.withRoutes(routes)],
      // ...
    });
    router = TestBed.inject(Router);
    location = TestBed.inject(Location);
    router.initialNavigation(); // 初始化导航
  });
  it('should navigate to specified path', fakeAsync(() => {
    router.navigate(['/specified-path']);
    tick();
    expect(location.path()).toBe('/specified-path');
  }));
});
RouterTestingModule的navigateByUrl方法:import { RouterTestingModule } from '@angular/router/testing';
describe('YourComponent', () => {
  let router: Router;
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [RouterTestingModule.withRoutes(routes)],
      // ...
    });
    router = TestBed.inject(Router);
    router.initialNavigation(); // 初始化导航
  });
  it('should navigate to specified path', fakeAsync(() => {
    router.navigateByUrl('/specified-path');
    tick();
    expect(router.url).toBe('/specified-path');
  }));
});
这些解决方法将帮助您在Angular单元测试中解决路由卡在根路径而不是重定向到指定路径的问题。