簡體   English   中英

如何使用Karma + Jasmine + AngularJS在測試中運行通用代碼?

[英]How can I run common code in tests using Karma + Jasmine + AngularJS?

我正在使用Karma對AngularJS應用程序運行單元測試。 問題是我使用ui-router插件,並且它發出一些XHR請求來運行模板這一事實迫使我模擬這些請求。 因此,我發現自己對每個測試文件都重復此步驟:

  beforeEach(function($templateCache) {
    $templateCache.put('templates/layout.html', '');
    $templateCache.put('templates/dashboard/index.html', '');
    $templateCache.put('templates/session/login.html', '');
  });

如何為所有單元測試運行這段代碼? 我嘗試了谷歌搜索,但沒有運氣。 另外,我應該以其他方式這樣做嗎? 請分享您的意見。

謝謝你們。

您應該查看ng-html2js-preprocessor:

https://github.com/karma-runner/karma-ng-html2js-preprocessor

它將把所有模板批處理成一個模板緩存模塊(在后台使用$ templateCache ),您可以使用:

describe('SOMETHING', function() {
    beforeEach(module('templates'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM