Angualr 单元测试 - 收到错误,因为"导航在 Angular 区域之外触发,您是否忘记调用"ngZone.run()"?



下面是我对单元测试的基本设置:

describe('ShellViewComponentAttributesComponent', () => {
let fixture: ComponentFixture<ShellSelectLanguageForEEComponent>;
let component: ShellSelectLanguageForEEComponent;
let store: MockStore<any>;
let location: Location;
let router: Router;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ShellSelectLanguageForEEComponent],
imports: [
[
RouterTestingModule.withRoutes([
{
path: 'SC/subject-curriculum',
component: ShellSelectLanguageForEEComponent
}
])
]
],
providers: [
provideMockStore({})
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
router = TestBed.get(Router);
location = TestBed.get(Location);
fixture = TestBed.createComponent(ShellSelectLanguageForEEComponent);
router.initialNavigation();
component = fixture.componentInstance;
store = TestBed.get(Store);
spyOn(store, 'dispatch');
}));

当我运行代码时,我得到多个错误,如:

console.warn node_modules/@angular/core/bundles/core.umd.js:25782
Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?

我没有触发任何导航。这是必须的吗?因为我已经加入了模块。如何处理这个问题。我遇到了搜索,但没有解决方案适合我。

你可以在下面的文档中找到答案https://dev.to/this-is-angular/testing-angular-routing-components-with-the-routertestingmodule-4cj0

将触发路由的代码封装在Zone中:enter code here

// @ts-ignore
fixture.ngZone.run(() => {
router.initialNavigation();
});

相关内容

  • 没有找到相关文章

最新更新