[英]Angular unit testing - stubbing/mocking a directive with reference in ViewChild
您如何存根/模拟读取为ViewChild
的指令/组件?
例如,使用来自 angular.io 的简单指令:
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor() { }
}
假设我正在测试AppComponent
并使用ViewChild
读取HighlightDirective
为:
@ViewChild(HighlightDirective) theHighlightDirective: HighlightDirective
存根指令是:
@Directive({
selector: '[appHighlight]'
})
export class StubbedHighlightDirective {
constructor() { }
}
由于组件正在尝试读取HighlightDirective
,即使您在单元测试中声明StubbedHighlightDirective
, theHighlightDirective
也将是undefined
。
例子:
it('HighlightDirective is defined', () => {
// This test fails
expect(component.theHighlightDirective).toBeDefined();
});
如果您忽略 tslint 中的某些内容或使用as
关键字,则可以解决此问题:
Version 1: Just ignore some things in tslint so compiler doesn't complain
it('HighlightDirective is defined', () => {
// Compiler will typically complain saying that
// StubbedHighlightDirective isn't assignable to type of HighlightDirective
component.theHighlightDirective = new StubbedHighlightDirective();
// this passes
expect(component.theHighlightDirective).toBeDefined();
});
Version 2: Use "as" keyword
it('HighlightDirective is defined', () => {
// Actually compiler will still complain with warnings
component.theHighlightDirective = new StubbedHighlightDirective() as HighlightDirective;
// this passes
expect(component.theHighlightDirective).toBeDefined();
});
有没有另一种方法可以彻底清除这些 ViewChild 引用?
问题是您正在使用一个类来查找孩子,而该类已被您的存根取代。 您可以在指令(文档、 博客文章)中使用匹配的exportAs
链接来确保真实版本和存根具有相同的名称。
在原始指令装饰器中:
@Directive({
selector: '[appHighlight]',
exportAs: 'appHighlight'
})
export class HighlightDirective {
在存根指令中:
@Directive({
selector: '[appHighlight]',
exportAs: 'appHighlight'
})
export class StubbedHighlightDirective {
然后在使用指令的模板中:
<div appHighlight #appHighlight="appHighlight">
完成所有这些后,您需要更新@ViewChild
定义以使用字符串而不是类:
@ViewChild('appHighlight') theHighlightDirective: HighlightDirective
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.