javascript - html 元素的单元测试用例

标签 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' 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/

相关文章:

javascript - 简单的 if else 结构。具有不同的 lang 属性

javascript - 我怎样才能使用 jquery slide toggle with class

angularjs - 如何在 Controller 中测试 Angular $destroy

javascript - Jasmine gem / Jasmine :ci : How to console. 日志?

javascript - Bloodhound:Twitter Typeahead 没有显示从远程/ajax 获取的所有结果

javascript - 从回调函数中获取返回值-JQuery

javascript - 试图将两个元素合二为一

javascript - 更改可折叠菜单标题中的文本会导致其消失

jquery - 将标题标签对齐为空格

javascript - 如何为包含 HTML 和 JavaScript 或 jQuery 的网页编写 Jasmine 测试?