How to fix "Error: Expected to be running in 'ProxyZone', but it was not found." in mocha testing?
How to fix "Error: Expected to be running in 'ProxyZone', but it was not found." in mocha testing?
我正在尝试使用 mocha、[= 测试我的英雄之旅 Angular 应用程序35=]柴&webpack。我遵循了 this post & also with the help of this guide,完成了设置并修复了构建错误并进行了测试 运行。
但是,我的测试失败了,除了我没有在 beforeEach
挂钩中使用 async()
的测试用例。
通过测试
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) DashboardComponent
"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)
我试过 因为我认为这是 zone.js 使用 mocha[= 的一些错误51=],但没有成功。
另外,我也试过按照Mocha Documentation进行异步测试,但是作为一个初学者,对我来说有点吃力。
我尝试过谷歌搜索,甚至在 stack-overflow 中,但关于 [=67= 的帖子数量有限] 用 mocha 测试。
感谢任何帮助解决这个问题。
我遇到了同样的问题,从 beforeEach
(和 it
)中删除 async
解决了问题。错误消息根本没有帮助,但我想从它生成的地方检测根本原因并不容易。
这是这个问题的答案。
基本上这个异常是因为 async 关键字的位置不正确。从 beforeEach 中删除异步并将其添加到 "it"
it('should be created', fakeAsync(() => {
expect(component).to.exist;
});
我在 describe
函数中意外使用 fakeAsync
时遇到了同样的问题。
删除它解决了我的问题。
我在这里看到了一些问题。
你有 2 个 beforeEach。你应该只有 1 个。
如果你的测试代码块有异步,那么你需要在一个函数上调用 await,and/or 使用 flush 或 tick 来推进时间。
我通常对可观察对象使用 fakeAsync,对真正的异步方法使用 async/await。
对于这个具体示例,您根本不需要异步。
2021
将下面的行作为第一次导入添加到规范文件中为我解决了这个问题:
import 'zone.js/dist/zone-testing';
我正在尝试使用 mocha、[= 测试我的英雄之旅 Angular 应用程序35=]柴&webpack。我遵循了 this post & also with the help of this guide,完成了设置并修复了构建错误并进行了测试 运行。
但是,我的测试失败了,除了我没有在 beforeEach
挂钩中使用 async()
的测试用例。
通过测试
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) DashboardComponent
"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)
我试过
另外,我也试过按照Mocha Documentation进行异步测试,但是作为一个初学者,对我来说有点吃力。
我尝试过谷歌搜索,甚至在 stack-overflow 中,但关于 [=67= 的帖子数量有限] 用 mocha 测试。 感谢任何帮助解决这个问题。
我遇到了同样的问题,从 beforeEach
(和 it
)中删除 async
解决了问题。错误消息根本没有帮助,但我想从它生成的地方检测根本原因并不容易。
这是这个问题的答案。
基本上这个异常是因为 async 关键字的位置不正确。从 beforeEach 中删除异步并将其添加到 "it"
it('should be created', fakeAsync(() => {
expect(component).to.exist;
});
我在 describe
函数中意外使用 fakeAsync
时遇到了同样的问题。
删除它解决了我的问题。
我在这里看到了一些问题。
你有 2 个 beforeEach。你应该只有 1 个。 如果你的测试代码块有异步,那么你需要在一个函数上调用 await,and/or 使用 flush 或 tick 来推进时间。 我通常对可观察对象使用 fakeAsync,对真正的异步方法使用 async/await。
对于这个具体示例,您根本不需要异步。
2021
将下面的行作为第一次导入添加到规范文件中为我解决了这个问题:
import 'zone.js/dist/zone-testing';