When button is clicked the count will be incremented and at the same time the count will be displayed in the html page in angular. For this condition I wrote the unit test using jasmine + Karma runner. But the expected value is not equal.
In testing : debug element module is used.
html code:
<button id="button1" (click)="count()">Click</button>
<h2>{{cnt}}</h2>
ts code:
cnt:number = 0;
count()
{
this.cnt+=1;
}
spec file:
it("check events with debugelement",()=>{
const fixture = TestBed.createComponent(AppComponent);
debug = fixture.debugElement;
const h2 = debug.query(By .css('h2'));
const btn = debug.query(By .css('#button1'));
btn.triggerEventHandler('click',{});
fixture.detectChanges();
let x = component.cnt;
let y = parseInt(h2.nativeElement.innerText);
expect(x).toBe(y);
})
error:
Expected 0 to be 1.
"I am getting different values". Can I know what mistake I have done?.