我正在尝试使用mocha、chai和webpack测试我的英雄之旅 Angular 应用程序。我已经关注了这篇文章并在本指南的帮助下完成了设置并修复了构建错误并进行了测试并运行。
async()
但是,我的测试失败了,除了我没有在beforeEach
钩子中使用的测试用例。
通过测试
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',但没有找到它,因为我认为这是zone.js使用mocha的一些错误,但没有成功。
另外,我尝试按照Mocha 文档进行异步测试,但作为初学者,这对我来说有点困难。
我尝试过谷歌搜索,甚至在stack-overflow中搜索过,但是关于使用mocha进行Angular 测试的帖子数量有限。感谢您提供任何帮助。