Angular NgModel双向绑定单元测试
Angular NgModel two-way binding unit test
我正在尝试测试Angular 2 中的双向绑定功能。我也阅读了其他一些答案,但仍然无法通过测试。
输入字段更新后,我想运行进行测试以确保 AppComponent class 上的 searchQuery 属性 与输入字段的值相同.
如前所述,我已经阅读了一些其他答案,并且随着我的阅读包括了额外的代码片段。那么目前有什么可能不是全部需要的?
组件
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: '<input type="text" name="input" [(ngModel)]="searchQuery" (change)="onChange()" id="search">',
styles: ['']
})
export class AppComponent {
public searchQuery: string;
onChange() {
console.log(this.searchQuery);
}
}
单元测试
import { ComponentFixture, TestBed, async, fakeAsync, tick, ComponentFixtureAutoDetect } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
describe('AppComponent', () => {
let comp: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ],
providers: [],
imports: [ FormsModule ],
schemas: []
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
comp = fixture.componentInstance;
});
it('should create the app', fakeAsync(() => {
const de = fixture.debugElement.query(By.css("#search"));
const el = de.nativeElement;
el.value = "My string";
var event = new Event('input', {
'bubbles': true,
'cancelable': true
});
el.dispatchEvent(event);
tick();
fixture.detectChanges();
expect(comp.searchQuery).toEqual("My string");
}));
});
如果有更好的方法,我当然很乐意就此得到任何反馈。
你必须运行
fixture.detectChanges();
在调度事件之前确保您的控件已经初始化并注册了 onChange
事件
setUpControl
函数
// view -> model
dir.valueAccessor.registerOnChange(function (newValue) {
dir.viewToModelUpdate(newValue);
control.markAsDirty();
control.setValue(newValue, { emitModelToViewChange: false });
});
另见
我正在尝试测试Angular 2 中的双向绑定功能。我也阅读了其他一些答案,但仍然无法通过测试。
输入字段更新后,我想运行进行测试以确保 AppComponent class 上的 searchQuery 属性 与输入字段的值相同.
如前所述,我已经阅读了一些其他答案,并且随着我的阅读包括了额外的代码片段。那么目前有什么可能不是全部需要的?
组件
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: '<input type="text" name="input" [(ngModel)]="searchQuery" (change)="onChange()" id="search">',
styles: ['']
})
export class AppComponent {
public searchQuery: string;
onChange() {
console.log(this.searchQuery);
}
}
单元测试
import { ComponentFixture, TestBed, async, fakeAsync, tick, ComponentFixtureAutoDetect } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
describe('AppComponent', () => {
let comp: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ],
providers: [],
imports: [ FormsModule ],
schemas: []
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
comp = fixture.componentInstance;
});
it('should create the app', fakeAsync(() => {
const de = fixture.debugElement.query(By.css("#search"));
const el = de.nativeElement;
el.value = "My string";
var event = new Event('input', {
'bubbles': true,
'cancelable': true
});
el.dispatchEvent(event);
tick();
fixture.detectChanges();
expect(comp.searchQuery).toEqual("My string");
}));
});
如果有更好的方法,我当然很乐意就此得到任何反馈。
你必须运行
fixture.detectChanges();
在调度事件之前确保您的控件已经初始化并注册了 onChange
事件
setUpControl
函数
// view -> model
dir.valueAccessor.registerOnChange(function (newValue) {
dir.viewToModelUpdate(newValue);
control.markAsDirty();
control.setValue(newValue, { emitModelToViewChange: false });
});
另见