2017-02-12 56 views
0

我使用茶匙作为测试运行器,该测试运行器加载所有模板文件(使用angular-rails-templates),这些文件使用类似这样的东西填充templates模块:如何测试使用templateUrl与angular-rails-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进行测试期间丢失了什么指令?

回答

0

您还没有关闭<sample>标签中$compile

尝试改变:

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

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

嗨,对不起这一点。我在过滤公开发布的代码时进行了一些拼写错误。我的测试是有效的,因为它使用'template'而不是'templateUrl'进行正确渲染 –