2013-10-14 29 views
26

我無法讓Karma適用於具有外部模板的指令。AngularJS + Karma + Ng-html2js =>無法實例化模塊... html

這裏是我的人緣配置文件:

preprocessors: { 
    'directives/loading/templates/loading.html': 'ng-html2js' 
}, 

files: [ 
    ... 
    'directives/loading/templates/loading.html', 
] 

ngHtml2JsPreprocessor: { 
    prependPrefix: '/app/' 
}, 

在指令文件:

... 
templateUrl: '/app/directives/loading/templates/loading.html' 
... 

在規範文件中:

describe('Loading directive', function() { 
    ... 
    beforeEach(module('/app/directives/loading/templates/loading.html')); 
    ... 
}); 

我收到以下錯誤:

Failed to instantiate module /app/directives/loading/templates/loading.html due to: Error: No module: /app/directives/loading/templates/loading.html

如果我修改了卡瑪 - NG-html2js預處理程序的源代碼,以打印生成的 文件的結果,我得到:

angular.module('/app/directives/loading/templates/loading.html', []).run(function($templateCache) { 
    $templateCache.put('/app/directives/loading/templates/loading.html', 
     '<div ng-hide="hideLoading" class="loading_panel">\n' + 
     ' <div class="center">\n' + 
     '  <div class="content">\n' + 
     '   <span ng-transclude></span>\n' + 
     '   <canvas width="32" height="32"></canvas>\n' + 
     '  </div>\n' + 
     ' </div>\n' + 
    '</div>'); 
}); 

如此看來,生成的JS文件是正確的但不是因果報應加載中...

另外,如果我用--log級調試,這裏涉及到模板中的臺詞:

DEBUG [preprocessor.html2js]: Processing "/home/rightink/public_html/bo2/master/web/app/directives/loading/templates/loading.html"

DEBUG [watcher]: Resolved files:

 /correct/path/to/the/app/directives/loading/templates/loading.html.js 

我失去的東西?

感謝,

+0

試圖找出通過在https://github.com/vojtajina/ng-directive-testing存儲庫。 – srigi

+1

我學會了用這個回購測試AngularJS指令。所以我從一開始就受到啓發。 – Vincent

+1

您是否找到解決方案? –

回答

3

這可能不是您的具體問題,但在我們的應用中,我們需要添加以下karma.conf.js:

ngHtml2JsPreprocessor: { 
    cacheIdFromPath: function(filepath) { 
     return '/vision/assets/' + filepath; 
    } 
} 

相應的預處理程序的設置是這樣的:

preprocessors: { 
    'views/**/*.html': 'html2js' 
}, 

我的理解是,這是由於在指定模板時使用AngularJS中的絕對URL--運行測試時哪些業務被重寫?

無論如何希望這有助於。

2

我正在學習AngularJS的過程中遇到同樣的問題。我不知道爲什麼,但改變karma.conf.js中的端口爲我解決了這個問題。

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

    ... 

    port: 9877, 

    ... 

    }); 
}; 

編輯:

有點更多的測試,我發現這個問題只發生在Chrome,並且被明確清除所有瀏覽器歷史記錄的解決後(Ctrl + F5鍵沒有工作)。

11

您可以預處理器緩存的模板模塊,然後可以前你的測試包括:

karma.conf。JS

files: [ 
    ... 
    'app/**/*.html' 
], 

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

ngHtml2JsPreprocessor: { 
    moduleName: 'templates' 
}, 

指令文件

... 
templateUrl: 'app/path-to-your/template.html', 
... 

規範文件

describe('My directive', function() { 

    beforeEach(module('templates')); 
    ... 
}); 
12

的問題可能是在file部分指定的相對路徑得到擴展到全的。

喜歡的東西directives/loading/templates/loading.html =>/home/joe/project/angular-app/directives/loading/templates/loading.html

...然後,模板得到與他們的完整路徑註冊。

解決方案是配置ng-html2js預處理器以刪除模板路徑的絕對部分。例如,在karma.conf.js文件中添加stripPrefix指令是這樣的:

ngHtml2JsPreprocessor: { 
    // strip this from the file path 
    stripPrefix: '.*/project/angular-app/' 
    prependPrefix: '/app/' 
} 

注意stripPrefix是一個正則表達式。

+0

感謝這篇文章,它幫助我解決了我的問題! –

相關問題