[角单位测试]:我如何在单元测试中模拟QueryList(无集成测试)
在内容投影方案中,我有以下方案:
// my-component.ts
@ContentChildren(SelectOption) selectOptions: QueryList<SelectOption>;
...
ngAfterContentInit() {
this.selectOptions.forEach((selectOption, i) => {
selectOption.index = i;
});
}
假设模板具有以下结构:
<ng-content select="select-option"></ng-content>
我试图以下面的方式模拟测试,但是找不到一种“添加”方法,使我可以添加子组件。
// my-component.spec.ts
component.selectOptions = {} as QueryList<SelectOption>;
但是我不知道如何在单元测试方案中添加投影组件(不是集成测试)
In a Content Projection scenario I have the following scenario:
// my-component.ts
@ContentChildren(SelectOption) selectOptions: QueryList<SelectOption>;
...
ngAfterContentInit() {
this.selectOptions.forEach((selectOption, i) => {
selectOption.index = i;
});
}
Assuming the template has the following structure:
<ng-content select="select-option"></ng-content>
I have tried to mock the test in the following way but I can't find an "add" method that allows me to add the child components.
// my-component.spec.ts
component.selectOptions = {} as QueryList<SelectOption>;
But I don't know how I can add the projected components in a unit test scenario (not an integration test)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
ng-mocks
to mockselectoption
。You can use
ng-mocks
to mockSelectOption
.使用object.sign()如下:
Use Object.assign() like below: