0

尝试对我的指令运行单元测试时,我不断收到此错误:

Error: Unexpected request: GET /assets/partials/project-brand.html
No more request expected

我对可能导致此问题的原因感到困惑。

这是我的 karma.conf.js:

files: [
  'js/*.js',
  'partials/*.html',
  '../tests/client/unit/*.js'
]
  preprocessors: {
  'partials/*.html': ['ng-html2js']
},

ngHtml2JsPreprocessor: {
  stripPrefix: 'public/'
}

我的文件组织:

-public
--js
---directives.js

--partials
---project-brand.html

-tests
--client
---unit
----directives.js

我的指令:

.directive('projectBrand', [function() {
  return {
    restrict: 'E',
    scope: {
      brand: '=',
      projectId: '=',
      index: '='
    },
    templateUrl: '/assets/partials/project-brand.html',
    controller: ['$scope', function($scope){
      $scope.isWorking = false;
    }]}}])

最后,我的指令测试文件:

beforeEach(module('app'));
beforeEach(module('partials/project-brand.html'));

beforeEach(inject(function(_$compile_, _$rootScope_){
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    $scope = $rootScope.$new();
    $scope.brand = {id: 1};
    $scope.project.id = 1;
    $scope.index = 0;

    element = angular.element('<project-brand brand="brand" project-id="project.id" index="$index"></project-brand>');
    directive = $compile(element)($scope);
    $scope.$apply();
}));

如果有人有一些建议,我将不胜感激!

4

2 回答 2

0

这是 Angular 如何处理 templateUrl 的问题之一。我遇到了同样的问题并修复了它查看您的文件结构,您可以尝试“partials/project-brand.html”吗?请让我知道这是否解决了它。

于 2015-07-21T19:43:00.743 回答
0

这是遇到类似问题的任何人的解决方法:

ngHtml2JsPreprocessor: {
  prependPrefix: '/assets/',
  moduleName: 'templates'
}

在指令测试脚本中:

beforeEach(module('templates'));
于 2015-07-23T22:00:32.263 回答