0

I'm using teaspoon as the test runner, which load all template files (using angular-rails-templates), which populate the templates module with stuff like this:

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

And my directive coffee is like this:

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

And my test coffee

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"

The directive will never render with this. If I replace the templateUrl with template content then it runs fine. But that is not useful at all. What am I missing to let the directive render during test with templateUrl?

Phương Nguyễn
  • 8,747
  • 15
  • 62
  • 96

1 Answers1

0

You haven't closed the <sample> tag in $compile

Try changing:

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

To

 element = $compile("<div><sample address='address'></sample></div>")(scope)
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Hi, sorry for this. I make some typos while filtering my code for public posting. My test was valid as it render properly with directive using `template` but not `templateUrl` – Phương Nguyễn Feb 13 '17 at 01:14