使用Jasmine进行角度测试无法比较html节点

Dar*_*ech 8 jasmine angularjs angularjs-directive

我正在使用Jasmine在Angular中构建指令测试.我有一个小例子测试,看起来像这样:

it("should compare html node", inject( function ($compile, $rootScope) {
  var elm = angular.element('<input>');
  elm = $compile(elm)($scope);
  $scope.$digest();
  console.log('btn', elm); // output: '<input class="ng-scope">'
  expect(elm).toBe('<input class="ng-scope">');

  expect(elm[0]).toBe('<input class="ng-scope">'); // these also fail
  expect(elm.html()).toBe('<input class="ng-scope">'); // ""
}));
Run Code Online (Sandbox Code Playgroud)

所以我得到了预期的输出到控制台,但Jasmine抱怨错误 Expected { length: 1, 0: HTMLNode } to be '<input class="ng-scope">'

我也试过使用elm[0]哪个给出了相同的错误,elm.html()但只返回一个空字符串.如何正确地将HTML节点与字符串进行比较?

NB我知道这是一个不切实际的测试,但我只想演示我当前的问题.

has*_*sin 12

所以榆树是angular.element一个jqLit​​e对象.正如您所指出的,您可以使用elm [0]来获取实际的dom元素.然后,您可以通过访问该字段来访问节点的html .outerHTML.所以我们最终的解决方案是使用

elm[0].outerHTML
Run Code Online (Sandbox Code Playgroud)