Angular 中的模板使用 Karma 进行测试,意外请求

Templates in Angular testing with Karma, Unexpected request

我想测试我的指令,它有一个模板。我将 Karma 与 Jasmine 框架一起使用,gulp 将所有 html 文件编译为 www/templates。当我 运行 测试时,我遇到了这个错误:

Error: Unexpected request: GET ../templates/directives/my_directive.html

这个文件有什么问题?

这是我的规格:

describe 'Directive: my-directive', ->
  beforeEach module 'myApp'
  beforeEach module 'myApp-templates'

  compile = {}
  rootScope = {}
  element = {}
  scope = {}

  beforeEach ->
    inject (_$compile_, _$rootScope_) ->

      compile = _$compile_
      rootScope = _$rootScope_
      scope = rootScope
      element = angular.element('<my-directive></my-directive>')
      compile(element)(scope)
      angular.element(document.body).append(element)
      scope.$apply()

  describe 'content', ->
    it 'should contain', ->
      console.log element

指令:

angular.module 'myApp.directives'

.directive 'myDirective', ->
  restrict: 'AE'
  replace: true
  templateUrl: '../templates/directives/my_directive.html'

和karma.conf.coffee:

files: [
  ...
  'www/templates/**/*.html'
  ...
]

preprocessors:
  'www/templates/**/*.html': ['ng-html2js']

ngHtml2JsPreprocessor:
  moduleName: 'myApp-templates'
  stripPrefix: 'www/'

只需删除指令的 templateURL 中的'../'...