【问题标题】:Only first test case succeeds - Jasmine/Karma in Angular只有第一个测试用例成功 - Angular 中的 Jasmine/Karma
【发布时间】:2018-09-03 07:42:40
【问题描述】:

我正在尝试使用 Jasmine/Karma 测试角度组件。当我为这个组件运行我的测试脚本时,只有第一个测试用例成功,剩下的都是错误,比如“Uncaught TypeError: Cannot read property 'coSearchCriteria' of undefined throw”。

我可以切换测试方法的顺序,第一个总是通过,即使最后一次运行失败。如果我一一运行这些测试用例,一切都会通过。但是通过“ng test”测试整个组件时出现问题。

我也以类似的方式测试了其余组件,但它们都工作正常。

【问题讨论】:

  • 看起来您的组件正在以静态方式共享某些内容,因此重新创建它没有任何意义..
  • 请提供您所有的相关组件代码。
  • @trichetriche 感谢您的回复。更新后,“coSearchCriteria”这是我得到的错误,仅在此文件中使用。
  • this.store.select(selectorCOCriteriaState) .subscribe((coSearchCriteria: SearchCriteriaState) → 你没有模拟你的商店服务,所以它的行为很糟糕,你无法控制它。模拟它以完全控制它。
  • 然后我建议你 read their guide 详细解释它的工作原理以及为什么要这样做。

标签: angular karma-jasmine


【解决方案1】:

您需要在规范文件中模拟您的 ngrx 存储。在测试任何存储时需要模拟该存储。

class MockStore {
  public dispatch(obj) {
    console.log('dispatching from the mock store!')
  }

  public select(obj) {
    console.log('selecting from the mock store!');

    return Observable.of({})
  }
}

【讨论】:

    猜你喜欢
    • 2018-01-31
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多