Javascript 用动态模板编写角度指令的测试

Javascript 用动态模板编写角度指令的测试,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,以下是指令: app.directive('templates',function() { return { restrict:'E', templateUrl: function(s,e) { switch (e.template) { case 'temp1': return 'temp1.html'; case 'temp2':

以下是指令:

app.directive('templates',function() {
return {
    restrict:'E',
    templateUrl: function(s,e) {
        switch (e.template) {
            case 'temp1':                   
                return 'temp1.html';
            case 'temp2':
                return 'temp1.htm2';
            default:
                // do nothing... ;
        }
    }
};
});

我可以在我的测试中编译它,但我不确定如何测试是否调用了正确的模板

这里没有太多要测试的内容。但作为一个感觉良好的测试,您可以将模板加载到缓存中,并在特定元素是否已呈现时进行测试,作为一个感觉良好的测试

例如:-

   describe('templates', function () {
    beforeEach(inject(function ($rootScope, $templateCache, $compile) {
        // Set an arbitrary template to test
        $templateCache.put('temp1.html', '<div class="test">Hello</div>');
        element = angular.element("<templates  template='temp1'></templates>");
        $compile(element)(scope);
        $rootScope.$digest();
    }));


    it('Should load template', function () {
       expect(element.find('.test').length).toEqual(1); //Test if element has loaded template properly
       expect(element.find('.test').text()).toEqual("Hello");
    });

然而,这里没有什么需要测试的,因为您只需要测试angular的templateUrl函数求值。

谢谢。我只是在把模板放到测试中时被难住了
.directive('templates',function() {
return {
    restrict:'E',
    templateUrl: function(e,attr) {
      return attr.template + ".html"
   };
});