2016-06-17 6 views
4

私はAngularJSアプリのカルマテストを設定しようとしていますが、何とか失敗しています。 私はすべてのステップhereを実行しましたが、ng-html2jsプリプロセッサが正しく動作していないと感じました。私はいつも、次のメッセージを取得しています:AngularJSカルマテスト:予期しないリクエスト:GET.template.html

Error: Unexpected request: GET js/templates/my-template.template.html

ここに私のコードの構造は、私がテストしようとしていますディレクティブがある

code structure

そして、ここに私のテスト構造

enter image description here

です:

angular 
    .module('myapp') 
    .directive('myDirective', ['myService', function (myService) { 
     return { 
      restrict: 'E', 
      templateUrl: 'js/templates/my-template.template.html', 
      scope: {}, 
      link: function (scope) { 
      } 
     } 
    }]); 

私のカルマconfには、次のようになります。このような

module.exports = function (config) { 
config.set({ 

    basePath: '../../', 

    frameworks: ['jasmine'], 


    files: [ 
     //some omitted like angular and so on... 
     'main/resources/static/**/*.js', 
     '**/*.html', 
     'test/js/**/*Spec.js' 
    ], 

    preprocessors: { 
     '**/*.html': ['ng-html2js'] 
    }, 

    ngHtml2JsPreprocessor: { 
     moduleName: 'templates' 
    } 
... (more default stuff omitted) 
} 

とmySpec.js:

describe('directive', function() { 
    'use strict'; 

    beforeEach(module('myapp')); 
    beforeEach(module('templates')); 

    var elm, 
     scope; 

    beforeEach(inject(function ($rootScope, $compile) { 
     elm = angular.element(
     '<my-directive>' + 
     '</my-directive>'); 

     scope = $rootScope.$new(); 

     $compile(elm)(scope); 
     scope.$digest(); 
    })); 

    describe('Does something', function() { 
     console.log('test'); 
     expect(true).toBe(true); 
    }); 
}); 

答えて

0

私はそれを持っていると思います。ソリューションはkarma.conf.jsにありました:

ngHtml2JsPreprocessor: { 
    cacheIdFromPath: function (filepath) { 
     //The suggested filepath.strip would through an error 
     var cacheId = filepath.replace('main/resources/static/', ''); 
     return cacheId; 
    }, 
    moduleName: 'templates' 
} 
1

あなたbeforeEach機能であなたのディレクティブのあなたのHTMLテンプレートのモックのテンプレートを追加します。使用する$templateCache

describe('directive', function() { 
    'use strict'; 

    beforeEach(module('myapp')); 
    beforeEach(module('templates')); 

    var elm, 
     scope; 

    beforeEach(inject(function ($rootScope, $compile, $templateCache) { 


     $templateCache.put('js/templates/my-template.template.html','HTML_OF_YOUR_DIRECTIVE'); 

     elm = angular.element(
     '<my-directive>' + 
     '</my-directive>'); 

     scope = $rootScope.$new(); 

     $compile(elm)(scope); 
     scope.$digest(); 
    })); 

    describe('Does something', function() { 
     console.log('test'); 
     expect(true).toBe(true); 
    }); 
}); 
+2

これを回避するためにng-html2jsは存在しないのですか?つまり、私の指示htmlはかなり大きいかもしれませんし、そこに貼り付けたくないのです。 – user3083022

+0

ng-html2jsによって生成されるファイルはどのフォルダですか? –

+0

それは私が自分自身に尋ねてきたことです。どうすればそれを見つけることができますか?ところで、 – user3083022

関連する問題