【问题标题】:Templates in Angular testing with Karma, Unexpected request使用 Karma 进行 Angular 测试中的模板,意外请求
【发布时间】:2015-04-11 00:40:52
【问题描述】:

我想测试我的指令,它有一个模板。我将 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/'

【问题讨论】:

    标签: angularjs coffeescript gulp karma-jasmine


    【解决方案1】:

    只需删除指令模板URL中的'../'...

    【讨论】:

      猜你喜欢
      • 2014-06-19
      • 1970-01-01
      • 2015-07-08
      • 2017-06-10
      • 1970-01-01
      • 2019-05-12
      • 2013-05-19
      • 2019-07-01
      • 2020-04-10
      相关资源
      最近更新 更多