2

there are a lot of questions about the ng-html2js plugin but unfortunately all answers didn't help me to solve y issue.

I followed the official installation guide and the example https://github.com/vojtajina/ng-directive-testing/blob/master/test/tabsSpec.js.

My goal is to test a directive that has a templateUrl property, this is my config

karma.conf.js

files: [
  ... // lots of studd here

  'app/views/**/*.html' // templates are all here
],

[...] // other karma configs here

preprocessors: {
  'app/views/**/*.html': ['ng-html2js']
},

The directive I would like to test it is really basic

'use strict';

angular.module('myAwesomeApp').directive('rzMenu', function () {
    return {
      templateUrl: 'views/directives/rzmenu.html',
      restrict: 'E'
    };   
});

The unit test file for this

'use strict';

describe('Directive: rzmenu', function () {

  // load the directive's module
  beforeEach(module('myAwesomeApp'));
  beforeEach(module('app/views/directives/rzmenu.html'));

  var element,
    scope;

  beforeEach(inject(function ($rootScope, $compile) {
    element = angular.element('<rzmenu></rzmenu>');
    scope = $rootScope.$new();
    element = $compile(element)(scope);
    scope.$digest();
  }));

  it('should have content', inject(function () {
    //scope.$digest();
    var content = element.text();
    expect(content).toBe('');
  }));
});

Look at the 'should have content' test, shouldn't be the content the same as the template?

Why I get and empty string?

Thanks in advance

1 Answer 1

4

Nevermind, there was 2 error that I solved:

1) cacheIds must match templateUrl property, so

stripPrefix: 'app/'

was needed in the configuration as well as call the module without the app/ prefix within the test.

beforeEach(module('views/directives/rzmenu.html'));

2) directive name is camelCase so the instance call was wrong, here below the correct one

element = angular.element('<rz-menu></rz-menu>');

:)

Sign up to request clarification or add additional context in comments.

2 Comments

For something that suppose to solve alot of headaches for testing it seems to cause alot just to set up! Thanks for the info
When in doubt add stripPrefix: 'app/'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.