fre*_*sko 9 javascript arrays unit-testing karma-jasmine
我的问题似乎很奇怪.我有一个带有一个新的,非常简单的函数的构造函数,它应该检查变量是否包含在数组中.它完美地工作(我在表单中使用此功能).
但是......我不能在这个函数上写任何单元测试,因为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)
};
Run Code Online (Sandbox Code Playgroud)
//测试(业力/茉莉花)
theConstructor.allNames = ["A", "B", "C"];
theConstructor.nameToBeChecked = "A";
var result theConstructor.isNameAlreadyUsed(); //error!
expect(result).toBeTruthy();
Run Code Online (Sandbox Code Playgroud)
茉莉花有可能看不到"包含"吗?数组被填充,变量也... ...为什么应该是那里的任何构造函数?
TypeError: undefined is not a constructor (evaluating 'vm.allNames.includes(vm.nameToBeChecked)
Run Code Online (Sandbox Code Playgroud)
谢谢
UPDATE
我注意到在茉莉花中,任何对"包含"的调用都会导致错误.这不取决于哪里.例如,它足以在jasmine文件中编写以下代码,以获得提及...构造函数的错误(?!?):
[1, 2, 3].includes(2);
// TypeError: undefined is not a constructor (evaluating '[1, 2, 3].includes(2)') in ...
Run Code Online (Sandbox Code Playgroud)
我猜这很可能是由于以下两件事之一造成的:
您的节点版本可能小于 6.0.0,因为Array.prototype.includes在 node@6.0.0 之前不受本机支持,或者
karma 配置文件中的浏览器设置为“IE”,因为 Internet Explorer 不支持Array.prototype.includes。
| 归档时间: |
|
| 查看次数: |
1572 次 |
| 最近记录: |