我如何在全局范围内 mock/replace 集成测试中的组件?

How can globally I mock/replace components in integration tests?

我有一个组件 "nested" 发出网络请求,我正在使用另一个组件 "parent" 中的这个组件。

我正在尝试为 "parent" 编写一些集成测试,但它们失败了,因为 "nested" 组件的 Web 请求失败了。

我没有模拟请求,而是希望模拟一些 "nested" 功能来阻止 Web 请求。这很容易通过 reopen 实现,但是这当然会导致 "nested" 的测试失败。

有谁知道我是否可以存根 "nested" 的片段,或者使用注册表将 "nested" 替换为扩展的 class?

要换出一个组件,仅用于单个测试模块,只需注册一个自定义组件来替换原来的组件(仅适用于测试模块的范围):

moduleForComponent('component-under-test', 'description', {
  integration: true,

  beforeEach() {
    this.container.registry.register('component:nested-component', NestedComponent.extend({
      modifiedFunction() {
      }
    }));
  }
});