我有一个Angular 8应用程序,它使用路由器在一些主要组件之间进行切换。 现在,我已经进行了一些单元测试,有时测试全部通过,但是有时一两个失败,并且总是有相同的原因。
这是我通过执行ng test(测试失败时)获得的输出:
Chrome 80.0.3987 (Windows 10.0.0): Executed 32 of 34 ERROR (0 secs / 0.819 secs)
Chrome 80.0.3987 (Windows 10.0.0) ERROR
An error was thrown in afterAll
Uncaught Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'overview'
Error: Cannot match any routes. URL Segment: 'overview'
at ApplyRedirects.noMatchError (http://localhost:9876/_karma_webpack_/node_modules/@angular/router/fesm2015/router.js:4295:1)
at CatchSubscriber.selector (http://localhost:9876/_karma_webpack_/node_modules/@angular/router/fesm2015/router.js:4259:1)
at CatchSubscriber.error (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/operators/catchError.js:29:1)
at MapSubscriber._error (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:75:1)
at MapSubscriber.error (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:55:1)
at MapSubscriber._error (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:75:1)
at MapSubscriber.error (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:55:1)
at MapSubscriber._error (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:75:1)
at MapSubscriber.error (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:55:1)
Chrome 80.0.3987 (Windows 10.0.0): Executed 34 of 34 ERROR (1.167 secs / 0.907 secs)
我在每个测试文件的imports数组中都包含了 RouterTestingModule ,其中相应的组件以任何方式使用路由器。 例如:
describe('FeedbackComponent', () => {
let component: FeedbackComponent;
let fixture: ComponentFixture<FeedbackComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ FeedbackComponent, OverviewHeaderComponent ],
imports: [FormsModule, RouterTestingModule, HttpClientTestingModule]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FeedbackComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
有人遇到过此类问题吗?还是知道为什么这是随机失败的? 已经非常感谢您:)
答案 0 :(得分:0)
尝试做:
RouterTestingModule.withRoutes(
[{path: 'overview', component: FeedbackComponent}]
)
真的,FeedbackComponent
可以是您创建的任何虚拟组件。这可能会解决您的问题,但是我不确定它将如何扩展。看来您有更大的潜在问题。我看到错误发生在afterAll
中,您是否有afterAll
个钩子?他们有什么有趣的事吗?