我不习惯测试或编写Angular代码,所以我想知道如何测试返回void并更改类变量的方法(在这种情况下为changeMyMonth
)?
date: Date = new Date();
private dateArray: Date[];
private getMaxDaysInMonth(): number {
return new Date(this.date.getFullYear(), this.date.getMonth() + 1, 0).getDate();
}
changeMyMonth(amount: number): void {
this.date= new Date(
this.date.getFullYear(),
this.date.getMonth() + amount, 1);
this.dateArray= this.getArrayOfDates(this.getMaxDays(), this.date);
}
答案 0 :(得分:0)
不是很漂亮,但是您可以通过使用强制转换为任何类型的“绕过”打字稿检查来访问私有字段;
let valueToCheck = (<any>yourComponent).dateArray;
private
修饰符对转译的javascript代码没有影响。
答案 1 :(得分:0)
beforeEach(() => {
fixture = TestBed.createComponent(ComponentName);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should test changeMyMonth', () => {
component.changeMyMonth(3);
fixture.detectChanges();
expect(component.date).toEqual(/* something */);
expect(component['dateArray']).toEqual(/* something */);
});
it('should test changeMyMonth', () => {
let returnValue = component['getMaxDaysInMonth']();
expect(returnValue).toEqual(/* something */);
});