【问题标题】:Unit test case for html elementhtml元素的单元测试用例
【发布时间】:2014-11-27 00:45:57
【问题描述】:

我无法使用 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
  });

错误:

TypeError: 'undefined' 不是函数(评估 'haystack.indexOf(needle)')

请帮我解决这个问题。

【问题讨论】:

    标签: javascript jquery jasmine jasmine-jquery


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多