如何修复此测试用例:无法绑定到 'value',因为它不是 'dls-option' 的已知 属性
How to fix this test case: Can't bind to 'value' since it isn't a known property of 'dls-option'
这是我使用 Jasmine 和 Karma 进行 Angular 单元测试的第 3 天。我正在关注 Pluralsight 讲座。首先,dls-option
是我从库中使用的一个组件。我会解释。我正在使用我们公司提供的 angular 组件库。在下面的代码中,<dls-dropdown>
和 <dls-option>
只不过是 HTML 的 <select>
和 <option>
标记。他们围绕它创建了颜色和字体样式包装器。这是我的代码:
timeselector.component.html
<div>
<h1>Choose one from the list</h1>
<dls-dropdown>
<dls-option *ngFor="let mode of modes" [value]="mode">
<p>{{ mode }}</p>
</dls-option>
</dls-dropdown>
</div>
timeselector.component.ts
import { Component, OnInit, ... } from '@angular/core';
...
@Component({
selector: 'app-timeselector',
templateUrl: './timeselector.component.html'
})
export class TimeselectorComponent implements OnInit {
modes = ["Novice", "Intermediate", "Expert", "Beast"];
...
}
这是我的测试文件:
timeselector.component.spec.ts
import { TestBed, ComponentFixture } from "@angular/core/testing"
import { TimeselectorComponent } from './timeselector.component'
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
fdescribe('TimeselectorComponent', () => {
let fixture: ComponentFixture<TimeselectorComponent>;
@Component({
selector: 'dls-label',
template: '<div></div>'
})
class DlsLabelComponent {}
@Component({
selector: 'dls-dropdown',
template: '<div></div>'
})
class DlsDropdownComponent {}
@Component({
selector: 'dls-option',
template: '<div></div>'
})
class DlsDropdownOption {}
beforeEach(()=>{
TestBed.configureTestingModule({
imports: [
FormsModule
],
declarations: [
TimeselectorComponent,
DlsLabelComponent,
DlsDropdownComponent,
DlsDropdownOption
]
});
fixture = TestBed.createComponent(TimeselectorComponent);
})
it('should create', ()=> {
//expect(fixture.componentInstance).toBeTruthy();
})
})
但是我的测试用例失败了。这是屏幕截图:
请帮我解决这个问题,也可以随时提出其他错误。这对我的事业有帮助。
PS: 我只是想做一个浅测试。我想模拟子组件。
我认为此处的错误消息非常准确 - dls-options
模拟组件丢失 @Input() value
.
timeselector.component.html
正在渲染 dls-option
并将 mode
传递到其 value
输入:
<dls-option *ngFor="let mode of modes" [value]="mode">
因此您需要在模拟中创建该输入:
@Component({
selector: 'dls-option',
template: '<div></div>'
})
class DlsDropdownOption {
@Input() value;
}
这是我使用 Jasmine 和 Karma 进行 Angular 单元测试的第 3 天。我正在关注 Pluralsight 讲座。首先,dls-option
是我从库中使用的一个组件。我会解释。我正在使用我们公司提供的 angular 组件库。在下面的代码中,<dls-dropdown>
和 <dls-option>
只不过是 HTML 的 <select>
和 <option>
标记。他们围绕它创建了颜色和字体样式包装器。这是我的代码:
timeselector.component.html
<div>
<h1>Choose one from the list</h1>
<dls-dropdown>
<dls-option *ngFor="let mode of modes" [value]="mode">
<p>{{ mode }}</p>
</dls-option>
</dls-dropdown>
</div>
timeselector.component.ts
import { Component, OnInit, ... } from '@angular/core';
...
@Component({
selector: 'app-timeselector',
templateUrl: './timeselector.component.html'
})
export class TimeselectorComponent implements OnInit {
modes = ["Novice", "Intermediate", "Expert", "Beast"];
...
}
这是我的测试文件: timeselector.component.spec.ts
import { TestBed, ComponentFixture } from "@angular/core/testing"
import { TimeselectorComponent } from './timeselector.component'
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
fdescribe('TimeselectorComponent', () => {
let fixture: ComponentFixture<TimeselectorComponent>;
@Component({
selector: 'dls-label',
template: '<div></div>'
})
class DlsLabelComponent {}
@Component({
selector: 'dls-dropdown',
template: '<div></div>'
})
class DlsDropdownComponent {}
@Component({
selector: 'dls-option',
template: '<div></div>'
})
class DlsDropdownOption {}
beforeEach(()=>{
TestBed.configureTestingModule({
imports: [
FormsModule
],
declarations: [
TimeselectorComponent,
DlsLabelComponent,
DlsDropdownComponent,
DlsDropdownOption
]
});
fixture = TestBed.createComponent(TimeselectorComponent);
})
it('should create', ()=> {
//expect(fixture.componentInstance).toBeTruthy();
})
})
但是我的测试用例失败了。这是屏幕截图:
请帮我解决这个问题,也可以随时提出其他错误。这对我的事业有帮助。
PS: 我只是想做一个浅测试。我想模拟子组件。
我认为此处的错误消息非常准确 - dls-options
模拟组件丢失 @Input() value
.
timeselector.component.html
正在渲染 dls-option
并将 mode
传递到其 value
输入:
<dls-option *ngFor="let mode of modes" [value]="mode">
因此您需要在模拟中创建该输入:
@Component({
selector: 'dls-option',
template: '<div></div>'
})
class DlsDropdownOption {
@Input() value;
}