【问题标题】:Issue with Directives Templates?指令模板有问题?
【发布时间】:2015-07-20 19:16:28
【问题描述】:

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

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();
}));

如果有人有任何建议,我将不胜感激!

【问题讨论】:

  • 我想你可能还有更多的指令测试文件。还是这样?
  • 是的,为了清楚起见,我会进行编辑,但没有太多其他内容
  • 只有 /assets 出现在你的 templateUrl 中。
  • @charlietfl 那么我应该改变什么?
  • 不确定服务器路由是如何配置的,但您是否尝试过不使用 templteUrl 中的 assets 目录?

标签: angularjs unit-testing angularjs-directive karma-jasmine ng-html2js


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    在指令测试脚本中:

    beforeEach(module('templates'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 2018-03-12
      相关资源
      最近更新 更多