在Karma Angular Unit Test期间'错误:意外请求'

 百变精灵621126 发布于 2023-01-17 11:56

运行时grunt karma,对其中一个指令的测试在尝试获取模板时失败.我使用ng-html2js作为预处理器.这是我的一些karma.conf.js

plugins: ['karma-chrome-launcher',
          'karma-jasmine',
          'ng-html2js',
          'karma-ng-html2js-preprocessor'],

preprocessors: {
  'app/scripts/directives/**/*.html': 'ng-html2js'
},

ngHtml2JsPreprocessor: {
  moduleName: 'templates'
}

在我的测试中,我有以下内容:

'use strict';

describe('Directive: myDirective', function () {

  // load the directive's module
  beforeEach(module('myApp'));
  beforeEach(module('templates'));

  var element,
    scope;

  beforeEach(inject(function ($rootScope) {
    scope = $rootScope.$new();
  }));

  it('should not show search area initially', inject(function ($compile) {
    element = angular.element('');
    element = $compile(element)(scope);
    scope.$digest();
    expect(element.find('.myClass').hasClass('myClass')).toBe(true);
  }));
});

当我运行测试时,我得到了

Error: Unexpected request: GET /scripts/directives/myDirective/myDirective.html

好像预处理器没有正确地注入模板的javascript版本.

我也尝试使用模板的路径,beforeEach(module(''));但会导致错误,内容如下:

Error: [$injector:modulerr] Failed to instantiate module...

我怎样才能解决这个问题?

1 个回答
  • 我有同样的问题.确保您具有完全匹配的文件.打开Goog​​le Chrome控制台并检查文件路径是否完全相同.

    在此输入图像描述

    在上面的例子中,我不得不在ngHtml2JsPreprocessor.stripPrefix中添加一个"/"字符串并且它有效.所以我想对于Yeoman,你应该使用

    ngHtml2JsPreprocessor: {
      moduleName: 'templates',
      stripPrefix: 'app/' //add a slash
    }
    

    2023-01-17 11:57 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有