模拟组件仍然包括实际组件
在我的单元测试中,我正在执行以下操作(通过 @ngneat/coptator):
const createComponent = createComponentFactory({
component: AppComponent,
detectChanges: false,
declarations: MockComponents(IgxToastComponent, NgxUiLoaderComponent),
imports: [
CommonModule, IgxToastModule, RouterTestingModule, NgxUiLoaderModule,
MockComponent(HeaderComponent)
],
mocks: [ToasterService]
})
HeaderComponent
是独立组件,因此根据NG-Mock网站上的说明,我将其包含在导入中。但是,当我运行测试时,我会从HeaderComponent
的构建中遇到错误,因为它包含了此组件不使用/模拟的其他服务。我的理解是,通过使用MockComponent
它应该已经伪造了组件,即使HTML仍然具有< app-header>
,它实际上并没有尝试加载。 HeaderComponent
。
我应该做不同的事情吗?
我正在使用NG Mocks 14.0.1
In my unit test I'm doing the following (via @ngneat/spectator):
const createComponent = createComponentFactory({
component: AppComponent,
detectChanges: false,
declarations: MockComponents(IgxToastComponent, NgxUiLoaderComponent),
imports: [
CommonModule, IgxToastModule, RouterTestingModule, NgxUiLoaderModule,
MockComponent(HeaderComponent)
],
mocks: [ToasterService]
})
The HeaderComponent
is a standalone component and so per the instructions on the ng-mock site I included it in the imports. When I run my tests though, I'm getting errors from the construction of the HeaderComponent
since it include other services that this component doesn't use/mock. My understanding is that by using MockComponent
it should have faked out the component so that even though the html still has <app-header>
it doesn't actually try to load the HeaderComponent
.
Am I supposed to be doing something differently?
I'm using ng-mocks 14.0.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您的组件是独立的,所以您需要使用
。它将保留ocalbuilder
AppComponent
,而其导入将被模拟:Because your components are standalone, you need to use
MockBuilder
. It will keepAppComponent
as it is, whereas its imports will be mocked: