我无法使用 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' is not a function (evaluating 'haystack.indexOf(needle)')
请帮我解决这个问题。
最佳答案
您正在尝试对 jQuery 节点集合执行 .toContain
测试,该集合不是数组,因此它没有 。您的错误提到的 indexOf
方法。如果您尝试使用 .val()
测试设置的值,请执行以下操作:
expect($('#current').val()).toEqual('');
关于javascript - html 元素的单元测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24243095/