Vai*_*hav 5 testing jasmine typescript karma-jasmine angular
嗨,我正在使用 Angular 和 TypeScript 开发一个应用程序。
以下是模板代码:
<input type="text" placeholder="Search Results" (input)="searchInput($event)">
Run Code Online (Sandbox Code Playgroud)
我关于 searchInput 方法的打字稿代码是:
searchInput(event: Event & { target: HTMLInputElement}) {
const { value } = event.target;
this.value = value;
// calling other method to reset values
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何在我的 spec.ts 文件中编写输入测试用例。
describe('Component', () => {
let component: Component;
let fixture: ComponentFixture<Component>;
const event = Event; // Is this fine ?
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
AppModule
],
providers: [
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(Component);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('searchInput() ', () => {
// how to approach
});
});
Run Code Online (Sandbox Code Playgroud)
请提供一种我将如何编写测试用例的方法。
这是编写简单测试用例的一种方法。
它能做什么:
value为假value输入元素的 并调度一个input eventvalue已正确更新代码(记得导入By):
...
import { By } from '@angular/platform-browser';
...
it('searchInput should update value when input changes', async(() => {
const fixture = TestBed.createComponent(AppComponent);
expect(fixture.debugElement.nativeElement.value).toBeFalsy()
const el: HTMLInputElement = fixture.debugElement.query(By.css('input')).nativeElement;
const testValue = 'some_value';
el.value = testValue;
el.dispatchEvent(new Event('input'));
expect(fixture.componentInstance.value).toEqual(testValue);
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3081 次 |
| 最近记录: |