angularjs - 如何在 Karma 单元测试中设置常量
问题描述
我在 Angular 1 中有这段代码。还有用 Karma 编写的测试用例。在这里,我已经涵盖了所有行。但是“如果”条件正在降低分支覆盖率。不知何故,我必须用“虚假”条件对其进行测试。
没有得到如何将“之后”常量作为未定义传递。
$scope.$on('klresource-bag.drop-model', (event, el, container) => {
const changedIndex = domIndexOf(el[0], container);
const changed = $scope.resources[changedIndex];
const after = $scope.resources[changedIndex - 1];
let afterId = 0;
if (!isUndefined(after)) {
afterId = after.model_resource.id;
}
$scope.$save(() => {
const s = KLResource.sort({
id: changed.model_resource.id,
after: afterId,
}, {});
return s.$promise;
});
});
这是我的单元测试用例。
describe('klresource-bag', () => {
it('Checks the event "klresource-bag.drop-model"', () => {
scope.$save = () => {};
spyOn(Array.prototype.indexOf, 'call').and.returnValue(1);
spyOn(scope, '$save').and.callFake(func => func());
spyOn(KLResource, 'sort').and.returnValue({ $promise: {} });
scope.$broadcast('klresource-bag.drop-model', {}, [{}], {});
scope.$apply();
expect(Array.prototype.indexOf.call).toHaveBeenCalled();
expect(scope.$save).toHaveBeenCalledWith(jasmine.any(Function));
expect(KLResource.sort).toHaveBeenCalledWith(jasmine.any(Object), jasmine.any(Object));
});
});
我尝试在我的单元测试中设置它,但它抛出错误:
scope.resources = undefined;
解决方案
推荐阅读
- java - 二维数组将一行中的所有数字相加,对角线索引除外
- swift - 在类中初始化数组 - Swift
- python - 如何使用 getattr 和 string 调用类?
- angular - Angular 8如何跳过不可见文本框的验证
- c++ - 如何优化我的代码以查找数组所有连续子序列的乘积?
- bash - 通过 SFTP 传输后创建文件
- c# - 如何修复 Visual Studio 2019 中的错误“windows sdk 未正确安装”?
- c++ - 无法为 Sublime Text 设置 EasyClangComplete
- flutter - 大写文本字段,但仅当我单击颤动中的按钮时
- csv - 如何读取 csv 文件并将值传递给 List
在颤抖?