首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“错误:意外请求”在卡玛角单元测试中的应用

“错误:意外请求”在卡玛角单元测试中的应用
EN

Stack Overflow用户
提问于 2014-05-02 13:17:13
回答 2查看 3.7K关注 0票数 6

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

代码语言:javascript
复制
plugins: ['karma-chrome-launcher',
          'karma-jasmine',
          'ng-html2js',
          'karma-ng-html2js-preprocessor'],

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

ngHtml2JsPreprocessor: {
  moduleName: 'templates'
}

在我的测试中,我有以下几点:

代码语言:javascript
复制
'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('<navbar></navbar>');
    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...

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-31 15:02:18

我也遇到了同样的问题。确保您有准确的文件匹配。打开Google控制台并检查文件路径是否完全相同。

在上面的示例中,我不得不在ngHtml2JsPreprocessor.stripPrefix中添加一个"/“字符串,它起了作用。所以我想对于约曼,你应该用

代码语言:javascript
复制
ngHtml2JsPreprocessor: {
  moduleName: 'templates',
  stripPrefix: 'app/' //add a slash
}
票数 9
EN

Stack Overflow用户

发布于 2014-05-02 14:37:11

由于我使用约曼工具为我的项目搭建脚手架,我需要在我的stripPrefix文件中的ngHtml2JsPreprocessor选项中添加一个karma.conf.js

代码语言:javascript
复制
ngHtml2JsPreprocessor: {
  moduleName: 'templates',
  stripPrefix: 'app'
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23429126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档