我无法使用 Jasmine在单元测试中获取html元素值。
JavaScript代码:
$scope.close = function () {
$scope.success = false;
$('#current, #new').val(''); // Need to test this
};
Jasmine代码:
it('close method', function() {
scope.close();
expect($('#current')).toContain(''); //unable to get the value ->return undefined
});
错误:
TypeError:' undefined'不是一个功能(评估' haystack.indexOf(针)')
请帮我解决这个问题。
答案 0 :(得分:2)
您正在尝试对jQuery节点集合执行.toContain
测试,该集合不是数组,因此它没有.indexOf
方法您的错误提到。如果您尝试使用.val()
测试您设置的值,请执行以下操作:
expect($('#current').val()).toEqual('');