【发布时间】:2019-11-25 11:55:52
【问题描述】:
我正在编写单元测试来检查组件是否成功创建。我看到以下错误 错误:模板解析错误:没有将“exportAs”设置为“matAutocomplete”的指令(“”auto” [formControl
这是我的包含自动完成指令的 template.html
<mat-form-field >
<input matInput [matAutocomplete]="auto" [formControl]="customerFilterControl">
<mat-autocomplete panelWidth ="450px" #auto="matAutocomplete" [displayWith] = "displayFn" style="width:750px;">
<mat-option *ngFor="let customer of filteredOptions | async" [value] ="customer.AccountID + '('+ customer.AccountName + ')'" (click)="onCustomerChange(customer)">
{{customer.AccountID}} ({{customer.AccountName}})
</mat-option>
</mat-autocomplete>
</mat-form-field>
这是单元测试spec.file,我尝试了以下的东西,
import { ActualComponent } from './ActualComponent';
import { NO_ERRORS_SCHEMA,CUSTOM_ELEMENTS_SCHEMA,Directive } from '@angular/core';
@Directive({
selector:'<matAutocomplete>',
})
export class matAutocomplete{}
beforeEach(()=>{
TestBed.configureTestingModule({
imports:[....],
declarations:[...matAutocomplete],(1)
...
schemas:[NO_ERRORS_SCHEMA,CUSTOM_ELEMENTS_SCHEMA] //this didn't fix (2)
})
it('should create', () => {
expect(component).toBeTruthy();
})
我希望测试能够通过 (1) 定义指令“matAutocomplete”并在规范文件中声明 (2) 在 testbed 配置中标记模式但是测试仍然没有通过!有人给我建议吗?
【问题讨论】:
-
declarations:[...matAutocomplete],(1)希望在 RL 中declarations: [matAutocomplete]...顺便说一句,我会使用 CamelCase 类名,以大写字母开头。
标签: javascript angular typescript unit-testing