2016-09-20 22 views
45

Tôi đang cố gắng kiểm tra ràng buộc hai chiều angular2 để điều khiển input. Dưới đây là các lỗi:thử nghiệm angular2: Không thể liên kết với 'ngModel' vì nó không phải là thuộc tính đã biết của 'đầu vào'

Can't bind to 'ngModel' since it isn't a known property of 'input'. 

Các app.component.html

<input id="name" type="text" [(ngModel)]="name" /> 
<div id="divName">{{name}}</div> 

Các 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'); 
    })); 
}); 

Trả lời

103

Bạn cần nhập FormsModule int o cấu hình TestBed.

import { FormsModule } from '@angular/forms'; 

TestBed.configureTestingModule({ 
    imports: [ FormsModule ], 
    declarations: [ 
    AppComponent 
    ], 
    providers:[AppService] 
}); 
+15

Công cụ góc cạnh này có vẻ ngẫu nhiên. Cảm ơn sự giúp đỡ của bạn. –

+0

cảm ơn người đàn ông. nó làm việc cho tôi trong thử nghiệm –

+2

Đồng ý, @PeteB. Dependency tiêm là tuyệt vời như vậy .... nó làm tất cả mọi thứ cho bạn automagically ... JUST DONT NHẬP KHẨU Ở ĐÂY VÀ NO_ERROR_SCHEMA và yada yda ... –

Các vấn đề liên quan