ng测试:抛出[object ErrorEvent]

时间:2018-10-10 10:24:35

标签: angular angular6 angular-test

我正在使用Angular 6

正在运行

ng test

我遇到类似的错误

ResetPasswordComponent should create
[object ErrorEvent] thrown

在窗口控制台中,它给出了

未捕获错误:未捕获(承诺中):错误:无法匹配任何路由。网址段:“ auth / login”

Error: Cannot match any routes. URL Segment: 'auth/login'
at ApplyRedirects.noMatchError (VM4374 router.js:1455)
at CatchSubscriber.eval [as selector] (VM4374 router.js:1436)
at CatchSubscriber.error (VM4260 catchError.js:40)
at MapSubscriber.Subscriber._error (VM4156 Subscriber.js:90)
at MapSubscriber.Subscriber.error (VM4156 Subscriber.js:70)
at MapSubscriber.Subscriber._error (VM4156 Subscriber.js:90)
at MapSubscriber.Subscriber.error (VM4156 Subscriber.js:70)
at MapSubscriber.Subscriber._error (VM4156 Subscriber.js:90)
at MapSubscriber.Subscriber.error (VM4156 Subscriber.js:70)
at TapSubscriber._error (VM4280 tap.js:67)
at ApplyRedirects.noMatchError (VM4374 router.js:1455)
at CatchSubscriber.eval [as selector] (VM4374 router.js:1436)
at CatchSubscriber.error (VM4260 catchError.js:40)
at MapSubscriber.Subscriber._error (VM4156 Subscriber.js:90)
at MapSubscriber.Subscriber.error (VM4156 Subscriber.js:70)
at MapSubscriber.Subscriber._error (VM4156 Subscriber.js:90)
at MapSubscriber.Subscriber.error (VM4156 Subscriber.js:70)
at MapSubscriber.Subscriber._error (VM4156 Subscriber.js:90)
at MapSubscriber.Subscriber.error (VM4156 Subscriber.js:70)
at TapSubscriber._error (VM4280 tap.js:67)
at resolvePromise (VM4143 zone.js:813)
at resolvePromise (VM4143 zone.js:770)
at eval (VM4143 zone.js:872)
at ZoneDelegate.invokeTask (VM4143 zone.js:420)
at ProxyZoneSpec.onInvokeTask (VM4148 zone-testing.js:318)
at ZoneDelegate.invokeTask (VM4143 zone.js:419)
at Object.onInvokeTask (VM4143 zone.js:298)
at ZoneDelegate.invokeTask (VM4143 zone.js:419)
at Object.onInvokeTask (VM4151 core.js:4109)
at ZoneDelegate.invokeTask (VM4143 zone.js:419)
at 

我创建了auth模块,其中包含组件ResetPasswordComponent

AuthModule是在auth-layout模块中导入的,路由是在auth-layout模块中定义的。

AuthModule

的内容
@NgModule({
  imports: [
    CommonModule,
    ReactiveFormsModule,
    FormsModule,
    RouterModule
  ],
  declarations: [
    LoginComponent,
    LogoutComponent,
    ForgotPasswordComponent,
    ResetPasswordComponent
  ],
  exports: [
    LoginComponent,
    ForgotPasswordComponent,
    ResetPasswordComponent,
    LogoutComponent
  ]
})
export class AuthModule { }

组件文件中没有auth/login

reset-password.component.html 文件包含一行

<a routerLink="/auth/login">Login</a>

1 个答案:

答案 0 :(得分:0)

这可能与OP无关,但对于遇到类似问题且未为other related questions提供解决方案的任何人申请,我将其留在这里。

就像OP一样,我在Jasmine窗口中出现了[object ErrorEvent] thrown错误,在控制台中也出现了伴随错误:

Uncaught Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'members'
Error: Cannot match any routes. URL Segment: 'members'
at ApplyRedirects.push../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.noMatchError
...

与OP一样,在规范或规范所属的组件中未引用错误中引用的路径。

在尝试修复该错误后,我最终决定禁用该规范。错误然后移至下一个规范。因此,我检查了报告为“故障”之前立即运行的规格和组件。当然,在前面的组件中,有一个对错误路径的引用,但是其规范并未在RouterTestingModule.withRoutes调用中注册该路径。

TLDR:

如果您具有 FirstComponent SecondComponent ThirdComponent 的规格,但是ThirdComponent在与上述类似的情况下失败,则检查 SecondComponent 以查看是否在此处引用了该路由。如果是,请创建一个存根组件,对其进行声明,然后使用缺少的路由将其添加到您的RouterTestingModule.withRoutes导入中。例如

...

describe('SecondComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
RouterTestingModule.withRoutes([{path:'missing/path',component:SecondComponent}])
      ],
      declarations: [ SecondComponent ]
    })
  })

  ...

})

@Component({selector:'app-second',template:''})
class SecondComponent {}

我希望这可以节省一些时间。我为此花了几个小时,主要是因为我从字面上看失败报告。