html元素的单元测试用例

时间:2014-06-16 11:52:36

标签: javascript jquery jasmine jasmine-jquery

我无法使用 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(针)')

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

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

expect($('#current').val()).toEqual('');
相关问题