【问题标题】:Test component with viewProvider使用 viewProvider 测试组件
【发布时间】:2021-04-27 09:49:27
【问题描述】:
@Component({
  selector: 'app-entity-details',
  templateUrl: './entity-details.component.html',
  styleUrls: ['./entity-details.component.scss'],
  viewProviders: [{
    provide: ControlContainer,
    useFactory: (container: ControlContainer) => container,
    deps: [[new SkipSelf(), ControlContainer]],
  }]
})
export class EntityDetailsComponent{

我发现上述设置在我的组件上使用 viewProviders 作为在嵌套组件中使用嵌套表单组的一种方式,而无需手动将它们作为输入和/或输出处理。现在我想弄清楚如何在规范文件中设置我的测试环境。

目前,我显然收到“未找到 'ControlContainer' 的提供程序”错误。配置 TestingModule 以提供与我使用的完全相同的东西,甚至是通用的 ControlContainer 都不会产生任何结果。我是否必须模拟一个使用我的EntityDetailsComponent 的新组件?

感谢任何可能提供帮助的资源。

【问题讨论】:

    标签: angular testing jasmine angular10


    【解决方案1】:

    编辑

    先看看这个link,我觉得它可以帮助你更多。

    修改

    尝试模拟它:

    let mockControlContainer;
    ....
    // This (mockControlerContainer = ) should be at the beginning of your first beforeEach
    // The first string is the identifier (can be anything) and the second array of strings
    // are public methods you would like to mock for ControlContainer.
    // I put 'public', 'methods' but you can change them to anything you like.
    mockControlContainer = jasmine.createSpyObj('container', ['public', 'methods']);
    ...
    TestBed.configureTestingModule({
      imports: [], // put in imports what is needed
      declarations: [EntityDetailsComponent],
      // provide the mockControlContainer
      providers: [{ provide: ControlContainer, useValue: mockControlContainer }]
    });
    

    如果ControlContainer没有任何依赖并且可以很容易地mock,你应该可以直接提供它:

    TestBed.configureTestingModule({
      imports: [], // put in imports what is needed
      declarations: [EntityDetailsComponent],
      // provide the mockControlContainer
      providers: [ControlContainer]
    });
    

    【讨论】:

    • 我不知道为什么我没有想到它,但是嘲笑它就可以了。愚蠢的我:D 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2015-11-05
    • 2021-10-02
    • 2021-06-29
    • 2019-12-21
    • 1970-01-01
    相关资源
    最近更新 更多