angular2 testing:无法绑定到'ngModel',因为它不是'input'的已知属性
我正在尝试为控制input
testingangular2双向绑定。 这是错误:
Can't bind to 'ngModel' since it isn't a known property of 'input'.
app.component.html
<input id="name" type="text" [(ngModel)]="name" /> <div id="divName">{{name}}</div>
app.component.ts
@Component({ selector: 'app-root', templateUrl: './app.component.html' }) export class AppComponent implements OnInit { name: string; }
app.component.spec.ts
import { TestBed, async } from '@angular/core/testing'; import { AppComponent } from './app.component'; import { AppService } from './app.service'; describe('App: Cli', () => { beforeEach(() => { TestBed.configureTestingModule({ declarations: [ AppComponent ], providers:[AppService] }); }); it('divName', async(() => { let fixture = TestBed.createComponent(AppComponent); let comp = fixture.componentInstance; comp.name = 'test'; fixture.detectChanges(); let compiled = fixture.debugElement.nativeElement; expect(compiled.querySelector('divName').textContent).toContain('test'); })); });
您需要将FormsModule
导入到TestBed
configuration文件中。
import { FormsModule } from '@angular/forms'; TestBed.configureTestingModule({ imports: [ FormsModule ], declarations: [ AppComponent ], providers:[AppService] });