Issue with Directives Templates?

120 Views Asked by At

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!

2

There are 2 best solutions below

0
On BEST ANSWER

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'));
0
On

This is one of the issues with how angular treats templateUrl. I faced the same issue and fixed it looking at your file structure can you please try 'partials/project-brand.html'. Please let me know if this fixed it.