我的问题似乎很奇怪。我有一个带有一个新的,非常简单的函数的构造函数,它应该检查变量是否包含在数组中。它完美地工作(我在表单中使用此功能)。
但是......我不能在这个函数上写任何单元测试,因为Karma / Jasmine看不到数组的“包含”功能。
有人可以建议我做什么吗?这里的情况,有点简化:
//要测试的构造函数
vm.isNameAlreadyUsed = function () {
//debut logging:
console.log ("vm.allNames ",vm.allNames); // output: vm.allNames ['A', 'B', 'C']
console.log ("and vm.nameToBeChecked is ",vm.nameToBeChecked); //output: and vm.nameToBeChecked is 'A'
return vm.allNames.includes(vm.nameToBeChecked);
// The previous works as expected at runtime, but it causes the following exception in karma/jasmine:
// TypeError: undefined is not a constructor (evaluating 'vm.allNames.includes(vm.nameToBeChecked)
};
//测试(业力/茉莉花)
theConstructor.allNames = ["A", "B", "C"];
theConstructor.nameToBeChecked = "A";
var result theConstructor.isNameAlreadyUsed(); //error!
expect(result).toBeTruthy();
茉莉花有可能看不到“包含”吗?数组已填充,变量也... ...为什么应该是那里的任何构造函数?
TypeError: undefined is not a constructor (evaluating 'vm.allNames.includes(vm.nameToBeChecked)
谢谢
UPDATE
我注意到在茉莉花中,任何对“包含”的调用都会导致错误。这不取决于哪里。例如,它足以在jasmine文件中编写以下代码,以获得提及...构造函数的错误(?!?):
[1, 2, 3].includes(2);
// TypeError: undefined is not a constructor (evaluating '[1, 2, 3].includes(2)') in ...
我猜这很可能是由于以下两点之一: