0

我正在尝试使用业力和茉莉花对所有指令进行单元测试。当我尝试加载具有名为 模板的指令时header.html,我收到以下错误:Error: Unexpected request: GET header.html No more request expected

http://plnkr.co/edit/YTUFXCMdK5JFEwMzzXaR?p=preview

更新:我在 karma.conf.js 中有以下配置:

files: [
      'test/client/specs/main.js',
      // 'WebContent/modules/common/header/**/*.html',
      {pattern: 'WebContent/libs/**/*.js', included: false},
      {pattern: 'WebContent/modules/**/*.js', included: false},
      {pattern: 'WebContent/modules/common/header/tmpl/*.html', included: false},
      {pattern: 'test/client/specs/**/*spec.js', included: false}
    ],


    // generate js files from html templates
    preprocessors: {
      'WebContent/modules/common/header/**/*.html': ['ng-html2js']
    },


    ngHtml2JsPreprocessor: {
      'moduleName': 'Templates',
      cacheIdFromPath: function(filepath) {
        return filepath.match(/\/WebContent\/modules\/common\/header\/.*\.html/);
      }
    },

我正在尝试通过以下方式加载它:

beforeEach(function() {
      module("Templates");
    });

现在我收到以下错误:

Error: [$injector:modulerr] Failed to instantiate module Templates due to:
    Error: [$injector:nomod] Module 'Templates' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    http://errors.angularjs.org/1.2.12/$injector/nomod?p0=Templates
4

2 回答 2

1

Karma 的方式是将模板 html 动态加载到 $templateCache 中。您可以只使用html2js业力预处理器,如此处所述

这归结为将模板“*.html”添加到您的 conf.js 文件中的文件中

preprocessors = {
  '*.html': 'html2js'
};

并使用

beforeEach(module('..'));

beforeEach(module('...html', '...html'));

进入你的 js 测试文件

于 2014-03-13T04:27:16.100 回答
1

我在单元测试中通过将 $templateCache 注入测试然后将 html 放入缓存中解决了这个问题。

http://plnkr.co/edit/btgYfiiRzacS6MfPFlbv?p=preview

我研究了几种不同的方法,我们决定将 html 放入指令中。

template: "<div>This is the template</div>"

它使测试变得更加容易,因为您不再需要在单元测试中更新 templateCache,当您的指令中有一大段 html 时,这很麻烦并且容易出错。

于 2014-03-16T17:48:50.827 回答