【发布时间】:2020-01-07 16:50:38
【问题描述】:
我有一个 Angular 8 应用程序,我试图在其中模拟一些子组件。
嘲笑本身很好。然而,为了组织和可重用性,我想将模拟从 .spec 文件中移出并移到它自己的模拟目录中,如下所示
myApp
|-- src
|-- app
|-- app.component.spec.ts
|-- mocks
|-- material
|-- mat-drawer-container.ts
MaterialDrawerContainer 的模拟如下所示:
import {Component, Input} from '@angular/core';
@Component({
selector: 'mat-drawer-container',
template: ''
})
export class MockMatDrawerContainer {
@Input() mode;
}
测试本身仍然按预期工作。但令人讨厌的是,MockMatDrawerContainer 下面有一条很大的旧红线,抱怨“MockMatDrawerContainer 未在任何角度模块中声明”,即使它在 testbed 配置测试模块中使用如下:
TestBed.configureTestingModule(
{declarations: [MockMatDrawerContainer]}).compileComponents;
我找到了关于“Compile on changes”的 stackoverflow 建议以及对 tsconfig.app.json 进行 compileOnSave 的相关更改,但除了在 mock 目录中创建一个虚假的 ngModule 之外没有任何帮助,这似乎很愚蠢。
有什么智慧之言吗?
【问题讨论】:
标签: typescript unit-testing jasmine