如何解决未定义的业力角4的原生元素

问题描述 投票:0回答:1

我收到这个错误,我无法解决,我已经尝试了一切可以任何人请帮我解决这个问题

这是我的html文件

<ng-template #icd10codes let-c="close" let-d="dismiss">
<form role="form" #icdCodeSelectionForm="ngForm" novalidate>
<div class="modal-header">
  <h5 class="modal-title">{{title}}</h5>
 </div>
 </form> 
 <ng-template>

我在ts文件中定义了标题

 public title: string = "Please Select ICD-10 Codes";

这是我的spec文件

BeforeEach(async() => {
fixture = TestBed.createComponent(NewTestOrderICD10CodeSelectionModalComponent);
component = fixture.debugElement.componentInstance;
de = fixture.debugElement.query(By.css('.modal-title')).nativeElement.innerText;
fixture.detectChanges();
});

it("New Test Order ICD10 Coed Selection Modal Should be created", () => {
expect(component).toBeTruthy();
});

it('Should have a title', () => {
//expect(element.textContent).toContain(component.title);
 expect(de).toContain(component.title);
});

我得到undefined的原生元素的错误有任何帮助来解决谢谢

javascript angular typescript unit-testing karma-jasmine
1个回答
0
投票

尝试:

it('Should have a title', () => {
  //expect(element.textContent).toContain(component.title);
  fixture.detectChanges();
  const de = fixture.debugElement.query(By.css('.modal-title')).nativeElement.innerText;    
   expect(de).toContain(component.title);
});

确保在访问元素之前调用了fixture.detectChanges();。这样可确保在呈现/测试html之前正确呈现所有角度值

去掉

de = fixture.debugElement.query(By.css('.modal-title')).nativeElement.innerText;

来自BeforeEach街区

© www.soinside.com 2019 - 2024. All rights reserved.