4 unit-testing jasmine typescript
我有一个 Angular 8 应用程序,我试图在其中模拟一些子组件。
\n嘲笑本身很好。但是,为了组织和可重用性,我想将模拟从.spec文件移到它自己的模拟目录中,如下所示:
\nmyApp\n|-- src\n |-- app\n |-- app.component.spec.ts\n |-- mocks\n |-- material\n |-- mat-drawer-container.ts\nRun Code Online (Sandbox Code Playgroud)\nMaterialDrawerContainer 的模拟如下所示:
\nimport {Component, Input} from \'@angular/core\';\n\n@Component({\n selector: \'mat-drawer-container\',\n template: \'\'\n})\n\nexport class MockMatDrawerContainer {\n@Input() mode;\n}\nRun Code Online (Sandbox Code Playgroud)\n测试本身仍然按预期工作。但令人烦恼的是,MockMatDrawerContainer 下面有一条旧的大红线,抱怨“MockMatDrawerContainer 未在任何角度模块中声明”,即使它在测试台配置测试模块中使用,如下所示:
\nTestBed.configureTestingModule(\n {declarations: [MockMatDrawerContainer]}).compileComponents;\nRun Code Online (Sandbox Code Playgroud)\n我找到了 Stack\xc2\xa0Overflow 对“更改时编译”的建议以及对compileOnSave 的 tsconfig.app.json 的相关更改,但除了在模拟目录中创建一个伪造的 ngModule 之外没有任何帮助,这看起来很愚蠢。
\n我怎样才能解决这个问题?
\n虽然您是正确的,正如您在答案中提到的那样,禁用 Angular 项目的所有此类检查,但禁用测试模拟的检查绝对是一个有效的用例。
这些警告来自代码检查(IntelliJ IDEA和WebStorm)。
要禁用任何检查(对于任何类型的项目):
在本例中,这将添加注释:
// noinspection AngularMissingOrInvalidDeclarationInModule
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1646 次 |
| 最近记录: |