【问题标题】:How to test angular directive that use templateUrl with angular-rails-templates如何测试将 templateUrl 与 angular-rails-templates 一起使用的 angular 指令
【发布时间】:2017-02-12 13:20:46
【问题描述】:

我使用 teaspoon 作为测试运行器,它会加载所有模板文件(使用 angular-rails-templates),这些模板文件会使用以下内容填充 templates 模块:

angular.module("templates").run(["$templateCache", function($templateCache) {
  $templateCache.put("directives/sample/tmpl.html", '<span>Hey</span>')
}]);

而我的指令咖啡是这样的:

angular.module 'myApp'
.directive 'sample', () ->
  restrict: 'E'
  templateUrl: 'directives/sample/tmpl.html'
  scope:
    address: "="
  controllerAs: 'vm'
  bindToController: true
  controller: () ->

还有我的测试咖啡

describe "sample directive", ->
  element = undefined

  beforeEach ->
    module("myApp", "templates")

  # Store references to $rootScope and $compile
  # so they are available to all tests in this describe block
  beforeEach ->
    inject ($compile, $rootScope) ->
      scope = $rootScope.$new()
      scope.address = "abcdef"
      element = $compile("<div><sample address='address'/></div>")(scope)
      scope.$digest()

  it "Replaces the element with the appropriate content", ->
    expect(element.html()).toContain "Hey"

指令永远不会用这个渲染。如果我将 templateUrl 替换为模板内容,那么它运行良好。但这根本没有用。在使用 templateUrl 进行测试期间让指令呈现我缺少什么?

【问题讨论】:

    标签: ruby-on-rails angularjs teaspoon


    【解决方案1】:

    你还没有关闭$compile中的&lt;sample&gt;标签

    尝试改变:

    element = $compile("<div><sample address='address'</div>")(scope)
    

     element = $compile("<div><sample address='address'></sample></div>")(scope)
    

    【讨论】:

    • 嗨,很抱歉。我在过滤我的代码以进行公开发布时出现了一些拼写错误。我的测试是有效的,因为它使用 template 而不是 templateUrl 的指令正确渲染
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 2015-07-11
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    相关资源
    最近更新 更多