Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript html元素的单元测试用例_Javascript_Jquery_Jasmine_Jasmine Jquery - Fatal编程技术网

Javascript html元素的单元测试用例

Javascript html元素的单元测试用例,javascript,jquery,jasmine,jasmine-jquery,Javascript,Jquery,Jasmine,Jasmine Jquery,我无法使用Jasmine在单元测试中获取html元素值。 JavaScript代码: $scope.close = function () { $scope.success = false; $('#current, #new').val(''); // Need to test this }; it('close method', function() { scope.close(); expect($('#current')).toContain

我无法使用Jasmine在单元测试中获取html元素值。

JavaScript代码:

  $scope.close = function () {
    $scope.success = false;
    $('#current, #new').val(''); // Need to test this
  };
  it('close method', function() {
    scope.close();
    expect($('#current')).toContain(''); //unable to get the value ->return undefined
  });
茉莉花代码:

  $scope.close = function () {
    $scope.success = false;
    $('#current, #new').val(''); // Need to test this
  };
  it('close method', function() {
    scope.close();
    expect($('#current')).toContain(''); //unable to get the value ->return undefined
  });
错误:

  $scope.close = function () {
    $scope.success = false;
    $('#current, #new').val(''); // Need to test this
  };
  it('close method', function() {
    scope.close();
    expect($('#current')).toContain(''); //unable to get the value ->return undefined
  });
TypeError:“undefined”不是函数(正在计算“haystack.indexOf(Pine针)”)


请帮助我解决这个问题。

您正在尝试对jQuery节点集合执行
.toContain
测试,该集合不是数组,因此它没有您的错误提到的
.indexOf
方法。如果您试图测试使用
.val()
设置的值,请改为:

expect($('#current').val()).toEqual('');