Jasmine tests AngularJS Directives with templateUr

2019-01-16 05:08发布

I'm writing directive tests for AngularJS with Jasmine, and using templateUrl with them: https://gist.github.com/tanepiper/62bd10125e8408def5cc

However, when I run the test I get the error included in the gist:

Error: Unexpected request: GET views/currency-select.html

From what I've read in the docs I thought I was doing this correctly, but it doesn't seem so - what am I missing here?

Thanks

8条回答
神经病院院长
2楼-- · 2019-01-16 05:57

If you are using the jasmine-maven-plugin together with RequireJS you can use the text plugin to load the template content into a variable and then put it in the template cache.


define(['angular', 'text!path/to/template.html', 'angular-route', 'angular-mocks'], function(ng, directiveTemplate) {
    "use strict";

    describe('Directive TestSuite', function () {

        beforeEach(inject(function( $templateCache) {
            $templateCache.put("path/to/template.html", directiveTemplate);
        }));

    });
});
查看更多
Rolldiameter
3楼-- · 2019-01-16 06:05

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

查看更多
登录 后发表回答