2

关于 ng-html2js 插件有很多问题,但不幸的是,所有答案都没有帮助我解决问题。

我遵循了官方安装指南和示例https://github.com/vojtajina/ng-directive-testing/blob/master/test/tabsSpec.js

我的目标是测试一个具有 templateUrl 属性的指令,这是我的配置

业力.conf.js

files: [
  ... // lots of studd here

  'app/views/**/*.html' // templates are all here
],

[...] // other karma configs here

preprocessors: {
  'app/views/**/*.html': ['ng-html2js']
},

我想测试的指令非常基本

'use strict';

angular.module('myAwesomeApp').directive('rzMenu', function () {
    return {
      templateUrl: 'views/directives/rzmenu.html',
      restrict: 'E'
    };   
});

这个的单元测试文件

'use strict';

describe('Directive: rzmenu', function () {

  // load the directive's module
  beforeEach(module('myAwesomeApp'));
  beforeEach(module('app/views/directives/rzmenu.html'));

  var element,
    scope;

  beforeEach(inject(function ($rootScope, $compile) {
    element = angular.element('<rzmenu></rzmenu>');
    scope = $rootScope.$new();
    element = $compile(element)(scope);
    scope.$digest();
  }));

  it('should have content', inject(function () {
    //scope.$digest();
    var content = element.text();
    expect(content).toBe('');
  }));
});

看'should have content'测试,内容不应该和模板一样吗?

为什么我得到空字符串?

提前致谢

4

1 回答 1

4

没关系,我解决了 2 个错误:

1) cacheIds 必须匹配 templateUrl 属性,所以

stripPrefix: 'app/'

在配置中需要,并且在测试中调用不带 app/ 前缀的模块。

beforeEach(module('views/directives/rzmenu.html'));

2)指令名称是camelCase所以实例调用是错误的,下面是正确的

element = angular.element('<rz-menu></rz-menu>');

:)

于 2014-09-08T15:06:21.293 回答