0

I'm trying to unit test all the directives using karma and jasmine. When I try to load a directive, which has a template called header.html, I get the following error: Error: Unexpected request: GET header.html No more request expected

http://plnkr.co/edit/YTUFXCMdK5JFEwMzzXaR?p=preview

Update: I have the following config in karma.conf.js:

files: [
      'test/client/specs/main.js',
      // 'WebContent/modules/common/header/**/*.html',
      {pattern: 'WebContent/libs/**/*.js', included: false},
      {pattern: 'WebContent/modules/**/*.js', included: false},
      {pattern: 'WebContent/modules/common/header/tmpl/*.html', included: false},
      {pattern: 'test/client/specs/**/*spec.js', included: false}
    ],


    // generate js files from html templates
    preprocessors: {
      'WebContent/modules/common/header/**/*.html': ['ng-html2js']
    },


    ngHtml2JsPreprocessor: {
      'moduleName': 'Templates',
      cacheIdFromPath: function(filepath) {
        return filepath.match(/\/WebContent\/modules\/common\/header\/.*\.html/);
      }
    },

I'm trying to load it by:

beforeEach(function() {
      module("Templates");
    });

Now i get the following errors:

Error: [$injector:modulerr] Failed to instantiate module Templates due to:
    Error: [$injector:nomod] Module 'Templates' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    http://errors.angularjs.org/1.2.12/$injector/nomod?p0=Templates

2 Answers 2

1

I solved this in my unit tests by injecting the $templateCache into the test and then putting the html into the cache.

http://plnkr.co/edit/btgYfiiRzacS6MfPFlbv?p=preview

I researched a few different approaches, and we settled on putting the html into the directive.

template: "<div>This is the template</div>"

It makes it much easier to test as you no longer need to update the templateCache in the unit test, which is a pain in the ass and error prone when you have a big piece of html in your directive.

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

6 Comments

Yea but I want to test all of my external templates. Sometimes I end up changing my templates when working with my source code. I don't want to put all of my templates in my unit test code.
Like I said then the least painful way is to put them in the directive directly (no pun intended). Feels a bit shitty though to put the html and JS together, but the benefits from unit testing seems to out weigh the other bad points.
I agree with you. At least I have it working right now. I was wondering what if I use $http to fetch the templates and put them in $templateCache in my unit test code one template at a time.
Could be an idea... I havent tried something like that, but it def seems to make the unit tests a bit more complex having to deal with asynchronous issues...
Meanwhile I will accept your answer and work with this. Thanks.
|
1

The Karma way is to load the template html dynamically into $templateCache. You could just use html2js karma pre-processor, as explained here

This boils down to adding templates '*.html' to your files in the conf.js file as well

preprocessors = {
  '*.html': 'html2js'
};

and use

beforeEach(module('..'));

beforeEach(module('...html', '...html'));

into your js testing file

Comments

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.