如何修复“错误:预期在“ProxyZone”中运行,但未找到。”在摩卡测试中?

2024-03-31

我正在尝试使用以下命令测试我的英雄之旅 Angular 应用程序mocha, chai & webpack。我已关注这个帖子 https://hichambi.github.io/2016/12/27/testing-angular2-with-webpack-mocha-on-browser-and-node.html并在以下人员的帮助下本指南 https://www.radzen.com/blog/testing-angular-webpack-mocha/,已完成设置并修复了构建错误并启动并运行了测试。

但是,除了我不使用的测试用例外,我的测试都失败了async() in the beforeEach hook.

通过测试

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [HeroService, MessageService]
    });
    httpTestingController = TestBed.get(HttpTestingController);

    let mockHeroes = [...mockData];
    let mockHero = mockHeroes[0];
    // let mockId = mockHero.id;

    heroService = TestBed.get(HeroService);
  });

  afterEach(() => {
    httpTestingController.verify();
  });

  it('is created', () => {
    expect(heroService).to.exist;
  });

测试失败

//imports
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { DashboardComponent } from './dashboard.component';
import { HeroSearchComponent } from '../hero-search/hero-search.component';

import { RouterTestingModule } from '@angular/router/testing';
import { HeroService } from '../hero.service';
import { expect } from 'chai';

...

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [DashboardComponent, HeroSearchComponent],
      imports: [RouterTestingModule.withRoutes([])],
      providers: [{ provide: HeroService, useValue: heroService }]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DashboardComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should be created', () => {
    expect(component).to.exist;
  });

错误堆栈跟踪

  1. 仪表板组件
  "before each" hook for "should be created":
Error: Expected to be running in 'ProxyZone', but it was not found.
 at Function.ProxyZoneSpec.assertPresent (node_modules\zone.js\dist\proxy.js:42:19)
 at runInTestZone (node_modules\zone.js\dist\async-test.js:202:23)
 at D:\Practice\Tour-Of-Heroes\node_modules\zone.js\dist\async-test.js:185:17
 at new ZoneAwarePromise (node_modules\zone.js\dist\zone-node.js:910:29)
 at Context.<anonymous> (node_modules\zone.js\dist\async-test.js:184:20)

我努力了Angular 5 测试:预计在“ProxyZone”中运行,但未找到 https://stackoverflow.com/q/48789782/8947616因为我认为这是一些错误zone.js using mocha,但没有成功。

另外,我尝试过以下摩卡文档 https://mochajs.org/#asynchronous-code对于异步测试,但作为初学者,这对我来说有点困难。

我尝试过谷歌搜索,甚至在 Stack Overflow 中,但关于使用 mocha 进行 Angular 测试的帖子数量有限。 任何帮助解决这个问题的帮助都是值得赞赏的。


我在无意中使用时遇到了同样的问题fakeAsync in a describe功能。

删除它为我解决了这个问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复“错误:预期在“ProxyZone”中运行,但未找到。”在摩卡测试中? 的相关文章

随机推荐