gpt4 book ai didi

javascript - 为什么文本在使用 toEqual 时不匹配 Angular ?

转载 作者:行者123 更新时间:2023-11-30 20:36:02 25 4
gpt4 key购买 nike

我正在尝试使用 jasmine 测试我的组件,但我不确定为什么我失败

这是y代码 https://stackblitz.com/edit/angular-bup8gb enter image description here

describe('initial display',()=>{
it('show counter text',()=>{
debugEl = fixture.debugElement.query(By.css('p.abc'));
el = fixture.nativeElement;
fixture.detectChanges();
expect(el.textContent).toEqual('counter 1')
})
})

最佳答案

这是执行所需操作的正确语法:

describe('initial display',()=>{
it('show counter text',()=>{
debugEl = fixture.debugElement.query(By.css('.abc'));
el = debugEl.nativeElement; //** You need to get the element from the degubEl
fixture.detectChanges();
expect(el.textContent).toEqual('counter 1')
})
})

这是另一种方法,但如果模板中有多个 p 元素,则可能不太理想:

describe('initial display',()=>{
it('show counter text',()=>{
el = fixture.nativeElement;
fixture.detectChanges();
expect(el.querySelector('p').textContent).toEqual('counter 1')
})
})

关于javascript - 为什么文本在使用 toEqual 时不匹配 Angular ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49826959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com