首页 > 解决方案 > 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()'?

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

标签: angularangular-unit-test

解决方案


推荐阅读