我有一個簡單的功能,可以在點擊事件上顯示和隱藏下拉選單。這是HTML代碼
<div (click)="toggleDropdown()" id="game-category">Show/Hide menu</div>
<div >
<app-category *ngIf="isShown" [filtersList]="categories">
</app-category>
</div>
該函式將相反的值設定為組件的屬性值。
isShown = false;
toggleDropdown(): void {
this.isShown = !this.isShown;
}
一切正常,但是當我運行測驗時出現錯誤。這是測驗代碼:
describe('CreateGameComponent', () => {
let component: CreateGameComponent;
let fixture: ComponentFixture<CreateGameComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
CommonModule,
],
declarations: [CreateGameComponent],
providers: [RequestService, SessionService, AdminService],
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(CreateGameComponent);
component = fixture.componentInstance;
component.isShown = false;
fixture.detectChanges();
});
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should check if toggleDropdownwas called', fakeAsync(() => {
spyOn(component, 'toggleDropdown');
let button =
fixture.debugElement.nativeElement.querySelector('#game-category');
button.click();
tick();
fixture.detectChanges();
expect(component.toggleDropdown).toHaveBeenCalled();
expect(component.isShown).toBe(true, 'isShown has not changed');
}));
})
觸發點擊事件isShown屬性后不會改變它的值。任何幫助表示贊賞。謝謝!
uj5u.com熱心網友回復:
當您這樣做時spyOn(component, 'toggleDropdown');,它會模擬該方法,其內容變為空,并且不再執行。您必須添加spyOn(component, 'toggleDropdown').and.callThrough(),因此實際上呼叫了該方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/407853.html
標籤:
上一篇:應該作業的簡單查詢不是
下一篇:Junit5對屬性測驗類執行測驗
