angular - Angular - 偶尔失败的单元测试(路由器)
问题描述
我有一个 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)
我已将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();
});
有人遇到过这种问题吗?或者知道为什么这会随机失败?已经非常感谢你了:)
解决方案
尝试做:
RouterTestingModule.withRoutes(
[{path: 'overview', component: FeedbackComponent}]
)
真的,FeedbackComponent
可以是您创建的任何虚拟组件。这可能会解决您的问题,但我不确定它将如何扩展。看来你有一个更大的潜在问题。我看到错误被抛出afterAll
,你有任何afterAll
钩子吗?他们有什么有趣的事情吗?
推荐阅读
- reactjs - How to get queryset in django rest framework for foreignkey?
- javascript - Facebook Login 不断重复询问权限 React Native?
- oracle - Not able to make new oracle db connection in sqldeveloper
- javascript - How do I create an express route that handles an HTTP GET request which sends an a href tag
- spring-boot - Vaadin BeanCreationException: during trying to call save method of my service class
- javascript - 如何制作和不和谐自动删除角色的 js 事件?
- php - 在 Laravel 6.2 中从空值创建默认对象
- android - 如何使用 Cloud Functions (Typescript) 在 Firebase 实时数据库中的 .ref(/path) 之外创建新的父节点?
- wordpress - How to show custom taxonomy in tab not working when using with wp post query?
- graph - 有趣的 Python 数据结构问题,涉及不相交集、散列和图