【发布时间】:2019-09-25 19:11:51
【问题描述】:
我正在尝试使用 mocha、chai 和 测试我的英雄之旅 Angular 应用程序webpack。我关注了this post 并在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) 仪表板组件
"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 Testing: Expected to be running in 'ProxyZone', but it was not found,因为我认为这是 zone.js 使用 mocha 的一些错误,但没有成功。
另外,我曾尝试关注Mocha Documentation 进行异步测试,但作为初学者,这对我来说有些困难。
我尝试过用谷歌搜索,甚至在 stack-overflow 中搜索过,但是关于 Angular 测试 和 的帖子数量有限>摩卡。 感谢您提供任何帮助解决此问题。
【问题讨论】:
-
我今天遇到的第 5 个 Angular 特定错误。这个框架真是太费时间了。
标签: angular typescript unit-testing mocha.js mocha-webpack