麻烦使用templateUrl的单元测试指令

时间:2014-05-26 14:00:44

标签: javascript angularjs unit-testing gruntjs karma-runner

我遇到使用templateUrl的单元测试指令时出现问题。

有关于AngularJS测试的这个很棒的教程[1],它甚至还有一个与之配合的Github回购[2]

所以我把它分叉[3]并进行了以下更改:

directives.js上我创建了两个新指令:

.directive('helloWorld', function() {
    return {
      restrict: 'E',
      replace: true,
      scope:{},
      template: '<div>hello world!</div>',
      controller: ['$scope', function ($scope) {}]
    }
})

.directive('helloWorld2', function() {
    return {
      restrict: 'E',
      replace: true,
      scope:{},
      templateUrl: 'mytemplate.html',
      controller: ['$scope', function ($scope) {}]
    }
})

我更改了test/unit/directives/directivesSpecs.js,以便将模板加载到$ templateCache中,然后再为新指令添加两个测试:

//
// test/unit/directives/directivesSpec.js
//
describe("Unit: Testing Directives", function() {

  var $compile, $rootScope, $templateCache;

  beforeEach(angular.mock.module('App'));

  beforeEach(inject(
    ['$compile','$rootScope', '$templateCache', function($c, $r, $tc) {
      $compile = $c;
      $rootScope = $r;

      //Added $templateCache and mytemplate
      $templateCache = $tc;
      $templateCache.put('mytemplate.html', '<div>hello world 2!</div>');
    }]
  ));

  //This was already here
  it("should display the welcome text properly", function() {
    var element = $compile('<div data-app-welcome>User</div>')($rootScope);
    expect(element.html()).to.match(/Welcome/i);
  });


  //Added this test - it passes
  it("should render inline templates", function() {
    var element = $compile('<hello-world></hello-world>')($rootScope);
    expect(element.text()).equal("hello world!");
  });

  //Added this test - it fails
  it("should render cached templates", function() {
    var element = $compile('<hello-world2></hello-world2>')($rootScope);
    expect(element.text()).equal("hello world 2!");
  });

});

最后一次测试失败,因为Angular不会像应该的那样编译模板。

$ grunt test:unit
Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.10 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 35.0.1916 (Linux)]: Connected on socket ChISVr0ZABZ1fusdyv3m
Chrome 35.0.1916 (Linux) Unit: Testing Directives should render cached templates FAILED
    expected '' to equal 'hello world 2!'
    AssertionError: expected '' to equal 'hello world 2!'
Chrome 35.0.1916 (Linux): Executed 18 of 18 (1 FAILED) (0.441 secs / 0.116 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

我很确定这应该有效。 至少,它与@SleepyMurth在[4]提出的解决方案非常相似。

但是我觉得我已经达到了解我目前对AngularJS的认识出了什么问题的极限。

HELP! : - )

[1] http://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-karma.html

[2] https://github.com/yearofmoo-articles/AngularJS-Testing-Article/

[3] https://github.com/tonylampada/AngularJS-Testing-Article

[4] Unit Testing AngularJS directive with templateUrl

1 个答案:

答案 0 :(得分:10)

问题

指定templateUrl后,将使用$http提取模板(即使是$http$templateCache提供的缓存模板)。出于这个原因,需要有一个$摘要周期,$http的承诺将通过模板内容解析并由指令处理。


解决方案

由于promises在$ digest循环期间得到解决(并且因为我们在“Angular context”之外),我们需要在评估断言之前手动调用$rootScope.$digest()
因此,最后一次测试应该像这样修改:

it("should render cached templates", function() {
    var element = $compile('<hello-world2></hello-world2>')($rootScope);
    $rootScope.$digest();   // <-- manually force a $digest cycle
    expect(element.text()).toBe(tmplText);
});

另请参阅此 short demo