0
例如,這個類:茉莉如何在不調用其他方法的情況下編寫jasmine中的單元測試和測試方法?
export class List<T> {
private _count: number;
private _items: Array<T>;
constructor() {
this._count = 0;
this._items = [];
}
get(index: number) {
return this._items[index];
}
add(item: T) {
this._items[this._count] = item;
this._count++;
}
}
這個單元測試:
import { List } from './list';
describe('List',() => {
let testList: List<string>;
beforeEach(() => {
testList = new List<string>();
});
it('get() method works',() => {
//how to test get() method without calling add() method?
testList.add("string1");
testList.add("string2");
testList.add("string3");
let s1: string = testList.get(0);
let s2: string = testList.get(1);
let s3: string = testList.get(2);
expect(s1).toBe("string1");
expect(s2).toBe("string2");
expect(s3).toBe("string3");
});
}
我想,這是不是單元測試,但集成測試,所以如何編寫正確的單元測試?我看着模擬,但我不知道,在這種情況下如何使用它。