Search code examples
angularjsunit-testingangularjs-directivekarma-jasmineng-html2js

Issue with Directives Templates?


I keep getting this error when trying to run unit-tests on my directives:

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

I'm stumped on what may be causing this issue.

Here is my karma.conf.js:

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

ngHtml2JsPreprocessor: {
  stripPrefix: 'public/'
}

My file organization:

-public
--js
---directives.js

--partials
---project-brand.html

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

My directive:

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

Lastly, my directives test file:

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

If anyone has some suggestions I would really appreciate it!


Solution

  • This was the fix for anyone who encounters a similar problem:

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

    And in the directives test script:

    beforeEach(module('templates'));