我有一个组件,我为此编写了两个测试用例。两个测试用例都使用setTimeout
。我注意到如果我一起运行测试用例(在单个describe
中运行,则测试用例会失败,但是如果将它们放在单独的describe
中,则测试用例会通过。为什么?这两个测试用例是
fit('should show dialog when message from DialogBoxService is received', () => {
let dialogBoxService: MockDialogBoxService = TestBed.get(DialogBoxService);
let dialogServiceContext = new DialogServiceContext(ComponentAndServiceID.QuestionDetailComponent, new QuestionDetailAdditionalInfo())
/*
At startup, there is a delay between creation of AppComponent and subscription to DialogService, I have added the test logic in setTimeout to give time for AppComponent to get created and also subscribe.
*/
setTimeout(() => {
spyOn(component, 'showDialog');
dialogBoxService.emitDialogMessage(dialogServiceContext);
expect(component.showDialog).toHaveBeenCalledWith(dialogServiceContext);
}, 1000);
});
fit('should handle message from AuthGuard service', () => {
let mockAuthGuard: MockAuthGuard = TestBed.get(AuthGuard);
spyOn(component, 'handleAuthGuardContextMessage');
let authGuardContext = new AuthGuardContext('You must sign in first', new AuthGuardAdditionalInfo());
setTimeout(() => {
mockAuthGuard.canActivate();
expect(component.handleAuthGuardContextMessage).toHaveBeenCalledWith(authGuardContext);
}, 1000);
});
我怀疑,问题与以下事实有关:两个测试用例都在1000毫秒后运行它们的expect
,而一个可能覆盖了其他情况,但是我不确定,因为我不知道{{ 1}}安排其他jasmine