В моем проекте Angular 5.2.0 у меня есть следующая структура:
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private _title = 'initial value';
public get title(): string {
return this._title;
}
public set title(v: string) {
this._title = v;
}
}
app.component.spec.ts
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { By } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [FormsModule]
}).compileComponents();
}));
it('should bind an input to a property', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
fixture.detectChanges();
// Update the title input
const inputElement = fixture.debugElement.query(By.css('input[name="title"]')).nativeElement;
inputElement.value = 'new value';
inputElement.dispatchEvent(new Event('input'));
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(app.title).toEqual('new value');
});
}));
});
И для следующих испытаний:
app.component.html
<input name="title" type="text" [(ngModel)]="title">
Но если я введу ввод в тег формы, тест не пройден:
app.component.html
<form>
<input name="title" type="text" [(ngModel)]="title">
</form>
Chrome 67.0.3396 (Windows 7 0.0.0) AppComponent должен связать входные данные со свойством FAILED
Ожидаемое «начальное значение» будет равно «новому значению».
Есть идеи, почему это происходит и как это исправить?