Angular2,Karma:fail()请求没有失败
我还在为Angular.io英雄教程的一个版本编写测试程序。在为组件编写测试时,我遇到了一些特殊的情况。我不能让这次考试不及格 例如:Angular2,Karma:fail()请求没有失败,angular,typescript,jasmine,karma-jasmine,Angular,Typescript,Jasmine,Karma Jasmine,我还在为Angular.io英雄教程的一个版本编写测试程序。在为组件编写测试时,我遇到了一些特殊的情况。我不能让这次考试不及格 例如: describe('OrgDetailComponent', () => { let comp: OrgDetailComponent; let fixture: ComponentFixture<OrgDetailComponent>; let de: DebugElement; let el: HTMLElement;
describe('OrgDetailComponent', () => {
let comp: OrgDetailComponent;
let fixture: ComponentFixture<OrgDetailComponent>;
let de: DebugElement;
let el: HTMLElement;
let org1: Org = new Org({ ... fills Org ... });
let dialogService: DialogService = null;
let globalsService: GlobalsService = null;
let orgService: OrgService = null;
let routeStub: any = { data: Observable.of( { org: org1 } ) } ;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ FormsModule, RouterTestingModule ],
providers : [
{ provide: DialogService, useClass: MockDialogService },
{ provide: GlobalsService, useClass: MockGlobalsService },
{ provide: OrgService, useClass: MockOrgService },
{ provide: ActivatedRoute, useValue: routeStub }
],
declarations: [ OrgDetailComponent ],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(OrgDetailComponent);
dialogService = fixture.debugElement.injector.get(DialogService);
globalsService = fixture.debugElement.injector.get(GlobalsService);
orgService = fixture.debugElement.injector.get(OrgService);
});
it('should always fail', () => {
fixture.detectChanges();
fixture.whenStable().then(() => {
fail('forced fail');
});
});
});
description('OrgDetailComponent',()=>{
让comp:OrgDetailComponent;
let夹具:组件夹具;
设de:DebugElement;
让el:HTMLElement;
设org1:Org=新组织({…填充组织…});
让dialogService:dialogService=null;
让globalsService:globalsService=null;
让orgService:orgService=null;
让routesub:any={data:Observable.of({org:org1})};
beforeach(异步(()=>{
TestBed.configureTestingModule({
导入:[FormsModule,RouterTestingModule],
供应商:[
{提供:DialogService,useClass:MockDialogService},
{provide:GlobalsService,useClass:MockGlobalsService},
{提供:OrgService,useClass:MockOrgService},
{provide:ActivatedRoute,useValue:RouteSub}
],
声明:[OrgDetailComponent],
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(OrgDetailComponent);
dialogService=fixture.debugElement.injector.get(dialogService);
globalsService=fixture.debugElement.injector.get(globalsService);
orgService=fixture.debugElement.injector.get(orgService);
});
它('应该总是失败',()=>{
fixture.detectChanges();
fixture.whenStable()然后(()=>{
失败(“强制失败”);
});
});
});
运行此测试,不会报告任何故障。用一个try-catch包住失败。调试器将在失败()时暂停。但是您没有输入catch()。因果报应总是报告成功。为什么
这与我有关,因为我无法证明我的测试是有效的。我正在编写多个测试,涉及这些fixture.whenStable()的克隆。我有明显的错误代码,但只要我只修复了一个错误,所有的错误信息都消失了。无法证明失败几乎使测试套件失效
谢谢你的回答,
Jerome。fixture.whenStable()
异步解析,因此需要使用async
import { async } from '@angular/core/testing'
it('should always fail', async(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
fail('forced fail');
});
}));
Angular被包装在async
中,将等待所有异步任务完成,然后让测试完成。如果没有async
,测试将在异步whenStable()解析之前完成
这就像在beforeach(async(()
)中一样。对compileComponents
的调用是异步解析的,因此可以将其包装在async
中,等待它完成
另一个选项是使用fakeAsync/tick
combo,我们可以使用它通过调用tick
强制解析异步任务,使测试“看起来”是同步的
import { fakeAsync, tick } from '@angular/core/testing'
it('should always fail', fakeAsync(() => {
fixture.detectChanges();
tick()
expect(...)
}));
假设fixture.detectChanges()
导致执行一些异步任务,则tick()
调用将等待该任务完成,因此现在我们可以将该测试视为同步测试
另请参见:
fixture.whenStable()
异步解析,因此需要使用async
import { async } from '@angular/core/testing'
it('should always fail', async(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
fail('forced fail');
});
}));
Angular被包装在async
中,将等待所有异步任务完成后再让测试完成。如果没有async
,则测试将在异步whenStable()解析之前完成
这就像在beforeach(async(()
)中一样。对compileComponents
的调用是异步解析的,因此可以将其包装在async
中,等待它完成
另一个选项是使用fakeAsync/tick
combo,我们可以使用它通过调用tick
强制解析异步任务,使测试“看起来”是同步的
import { fakeAsync, tick } from '@angular/core/testing'
it('should always fail', fakeAsync(() => {
fixture.detectChanges();
tick()
expect(...)
}));
假设fixture.detectChanges()
导致执行一些异步任务,则tick()
调用将等待该任务完成,因此现在我们可以将该测试视为同步测试
另请参见:
你有教程网站吗?我的问题总是由你来回答!不,没有“教程网站”。我有一个,但我在这方面没有在Hereo上那么活跃。你有教程网站吗?我的问题总是由你来回答!不,没有“教程网站”。我有一个,但我在这方面没有在这里那么活跃