typescript 單元測試 測試private protected 方法

最開始使用的爲people[‘excrete’]或people: any = new People();這兩種方法來測試,但是問題是當方法的參數變更測試文件不能檢查到。
更好的方法:
需要測試的class

class People {
	protected status: string;
	construct(public name: string, sex: 'male'|'female', age: number) {
	}
	private excrete(): void {
		this.status = '撒尿中';
	}
	public talk(who: string) {
		this.status = `正在與${who}談話中`;
	}
}

測試

describe('people unit test', () => {
	it('function excrete test', () => {
		let people = new People('Peter', 'male', 18);
		// @ts-ignore
		people.excrete();
		// @ts-ignore
		expect(people.status).toEqual('撒尿中');
	})
});
發佈了99 篇原創文章 · 獲贊 20 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章