我编写了一个方法,如果要添加的模式中还有文件夹,则返回true;如果模式中没有要添加到应用程序的文件夹,则返回false
我在angular / typesrcipt中的方法
ifAllFoldersHaveBeenAdded() {
let added = false;
let folderToExclude = find(this.bdDataModel.lenderDefinedFolders, {
name: 'Correspondence' });
let foldersToBeChecked = without(this.bdDataModel.lenderDefinedFolders, folderToExclude);
for (let i = 0; i < foldersToBeChecked.length; i++) {
let folderPresent = find(this.bdDataModel.folders, { name: foldersToBeChecked[i].name });
if (folderPresent) {
added = true;
} else {
added = false;
return added;
}
}
return added;
}
我的考试......
describe('ifAllFoldersHaveBeenAdded()', () => {
it('returns false if there are folders left in the modal to be added', () => {
let added = false;
$ctrl.bdDataModel.folders;
$ctrl.foldersToBeChecked = [{ name: 'FMS' }, { name: 'Other' }];
$ctrl.folderPresent = { name: 'FMS' };
$ctrl.ifAllFoldersHaveBeenAdded();
added = true;
expect(added).toEqual(true);
});
it('returns true is all folders have been added to the application', () => {
let added = false;
$ctrl.bdDataModel.folders;
$ctrl.foldersToBeChecked = [{ name: 'FMS' }, { name: 'Other' }];
$ctrl.folderPresent = { name: 'LMI' };
$ctrl.ifAllFoldersHaveBeenAdded();
expect(added).toEqual(false);
});
});
两个测试都通过但icov覆盖率报告将以下行标记为未覆盖...
find(this.bdDataModel.folders, { name: foldersToBeChecked[i].name });
if (folderPresent) {
added = true;
}
else {
added = false;
return added;
}
请告诉我应该在测试中添加什么,以便为此单元测试提供100%的测试覆盖率。 提前谢谢
答案 0 :(得分:1)
你的测试有很多错误。首先,您需要完全摆脱本地added
变量。做类似以下的事情
added = true;
expect(added).toEqual(true);
与执行expect(true).toBe(true)
接下来,folderPresent
和foldersToBeChecked
变量是ifAllFoldersHaveBeenAdded
函数的本地变量。它们不是控制器变量。您不需要在测试中设置$ctrl.folderPresent
或$ctrl.foldersToBeChecked
。
您需要定义这两个变量$ctrl.bdDataModel.lenderDefinedFolders
和$ctrl.bdDataModel.folders
由于lenderDefinedFolders
未定义,函数中foldersToBeChecked
的长度将为0.因此for
循环将永远不会执行。
您的测试应该如下所示:
it('returns false if there are folders left in the modal to be added', () => {
$ctrl.bdDataModel.lenderDefinedFolders = [{ //add lender defined folders here }]
$ctrl.bdDataModel.folders = [{ //add some folders here}]
let added = $ctrl.ifAllFoldersHaveBeenAdded();
expect(added).toEqual(true);
});