[英]Unable to access an element inside ng-template while writing unit test using Jasmine
所以我試圖訪問 ng-container 內的按鈕,但即使在手動將 ngIf 條件的值設置為 true 之后,元素內的元素也不會在我的測試環境中呈現。 到目前為止我已經嘗試過
似乎沒有任何效果,我不斷收到“TypeError:無法讀取 null 的屬性(讀取'nativeElement')”
it('should call assignGroup function', fakeAsync(() => { component.Show = true; fixture.detectChanges(); let button = fixture.debugElement.query(By.css('#assignGrpBtn')).nativeElement; console.log(button); // getting null for the btn }));
<ng-container *ngIf="Show"> <ng-template pTemplate="caption"> <div style="float: left;"> <button class="btn btn-secondary" (click)="assignGroup()" [ngClass]="{'assignGrpDisabled': assignToGrpBtnStatus,'assignToGrpBtn':!assignToGrpBtnStatus}" id="assignGrpBtn"><i class="fa fa-plus-square fa-lg"></i> Assign to Group(s)</button> </div> </ng-template> </ng-container>
我認為您的TestBed
不知道如何呈現pTemplate
指令。
你必須做這樣的事情(我假設你正在使用 prime-ng):
import {TableModule} from 'primeng/table';
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
YourComponent,
// If including TableModule in imports does not work, try including
// the directive for pTemplate.
// PTemplateDirective
],
imports:[TableModule]
}).compileComponents();
}));
一旦TableModule
被導入並添加到imports
數組中,測試應該知道如何繪制它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.