【发布时间】:2021-07-16 22:06:18
【问题描述】:
我正在开发一个 VS Code 扩展,目前我正在进行单元测试。我对单元测试完全陌生,我对如何对 void 函数进行单元测试非常迷茫。
export const getChecklistItems = async (id: number): Promise<any> => {
const checklistItems: ChecklistItem[] = [];
const items: any[] = await getItemsFromUrl(`path_to_url/{id}`);
items.map((item) => checklistItems.push(new ChecklistItem(item.checklist_items_id, item.checklist_items_content)));
return checklistItems;
};
export const onSelectInsertComment = async (id: number): Promise<void> => {
const editor: TextEditor | undefined = window.activeTextEditor;
const items: any[] = await getChecklistItems(id);
items.reverse(); // Workaround to output comments in correct numerical order inside the TextEditor
if (!editor) {
window.showWarningMessage('No editors are open in you workspace');
} else {
items.forEach((item) => {
editor?.insertSnippet(new SnippetString(`$LINE_COMMENT ${item.label}\n`));
});
window.showInformationMessage('Great. Checklist items have been inserted');
}
};
我正在使用 mocha、chai 和 sinon 进行单元测试。我正在尝试使用间谍检查在调用onSelectInsertComment(id) 时是否调用了getCheckListItems(id)。我尝试过的事情之一如下:
describe('#onSelectInsertComment', () => {
it('should be resolved', () => {
const id = 1;
const spy = sinon.spy(getChecklistItems);
spy.withArgs(id);
onSelectInsertComment(id);
sinon.assert.calledOnce(spy);
});
});
结果是: AssertError: expected getChecklistItems to be called once but was called 0 times。
我做错了什么?如果我被指出正确的方向,我将不胜感激。谢谢。
【问题讨论】:
标签: javascript typescript unit-testing sinon vscode-extensions