我正在嘗試創建覆寫所有行(茉莉花/業力),但我收到錯誤,因為無法讀取未定義的屬性“首選項”
這是我的組件代碼代碼。
export class PreferComponent implements OnInit {
@Output() updatePreferences = new EventEmitter<any>();
@ViewChild('settingModal', { static: true }) settingModal: any;
private data: any;
preferences = [];
maxCols: number;
selectedPref: number;
constructor() { }
ngOnInit(): void {
this.preferences = JSON.parse(JSON.stringify(this.data.preferences));
this.maxCols = this.data.maxCols;
this.selectedPref = this.data.selectedPref;
this.checkPreferences();
}
setPreferences(col, val) {
if (val) {
col.isPreferredColumn = true;
this.selectedPref ;
} else {
col.isPreferredColumn = false;
this.selectedPref--;
}
this.checkPreferences();
}
}
這是我嘗試過的規范的以下代碼:
fdescribe('PreferComponent ', () => {
let component: PreferComponent ;
let fixture: ComponentFixture<PreferComponent >;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [PreferencesComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(PreferComponent );
fixture.detectChanges();
component = fixture.componentInstance;
const data1 = {
preferences: [1, 2, 4],
"maxCols": 5,
"selectedPref": 7
};
component.preferences = data1.preferences;
component.maxCols = data1.maxCols;
component.selectedPref = data1.selectedPref;
expect(component.preferences).toEqual([1, 2, 4]);
component.ngOnInit();
});
});
我在這里做錯了什么?
uj5u.com熱心網友回復:
在該ngOnInit方法中,您使用this.preferences從this.data.
因此,在beforeEach函式中,您應該進行以下更改:
代替...
component.preferences = data1.preferences;
...寫這個:
@ts-ignore
component.data = data1;
此外,您應該繼續前進
expect(component.preferences).toEqual([1, 2, 4]);,component.ngOnInit();因為這會檢查是否ngOnInit完成了您的預期。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/359945.html
下一篇:如何模擬模塊級實體
