最開始使用的爲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('撒尿中');
})
});