在测试角度指令时,即使用ng-html2js,也可以使用Karma'意外请求'

时间:2014-04-04 17:49:32

标签: angularjs unit-testing angularjs-directive karma-runner

在最后一天尝试完成这项工作之后,我发现我已经绕回到我在开始时遇到的同样错误:

错误:意外请求:GET test-directive.html

我正在使用Karma和Jasmine来测试Angular中的指令。我在StackOverflow上查看了类似的问题,但发现在其他示例中尝试过的所有内容都无济于事。

代码结构

测试应用内
-src
--bower
--lib
--js
--modules
--- TESTDIR
---- test.js
----测试directive.html
----测试
----- test.spec.js
-test
--config
--- karma.conf.js
--e2e

Karma Config

'use strict';
module.exports = function(config){
    config.set({
    basePath: '../../',
    frameworks: ['jasmine'],
    files: [
        // Angular
        'src/bower/angular/angular.js',
        // Mocks
        'src/bower/angular-mocks/angular-mocks.js',
        // Libraries
        'src/lib/**/*.js',
        // App
        'src/js/*.js',
        'src/modules/*/*.js',
        // Tests
        'src/modules/**/test/*spec.js',
        // Templates
        'src/modules/**/*.html'
    ],
    autoWatch: false,
    singleRun: true,
    reporters: ['progress'],
    browsers: ['PhantomJS'],

    preprocessors: {
        'src/modules/**/*.html': 'ng-html2js'
    },
    ngHtml2JsPreprocessor: {
        moduleName: 'dir-templates'
    },
    plugins: [
        'karma-jasmine',
        'karma-ng-html2js-preprocessor',
        'karma-phantomjs-launcher',
        'karma-chrome-launcher',
        'karma-junit-reporter'
    ]
    });
};

test.js

'use strict';
angular.module('modules.test', []).
directive('testDirective', [function() {
    return {
        restrict: 'E',
        templateUrl: 'test-directive.html',
        link: function($scope, $elem, $attrs) {
            $scope.someFn = function() {
                angular.noop();
            };
        }
    };
}]);

测试direct.html

<span>Hello World</span>

test.spec.js

'use strict';
describe('test module', function() {
    beforeEach(module('modules.test'));
    /* -- DIRECTIVES------------------ */
    describe('directives', function() {
        var $compile, $scope, elm;
        beforeEach(module('dir-templates');
        beforeEach(inject(function($compile, $rootScope) {
            $scope = $rootScope.$new();
            elm = angular.element('<test-directive></test-directive>');
            $compile(elm)($scope);
            $scope.$digest();
        }));
        it('should have one span tag', function(){
            //Jasmine test here to check for one span tag.
        });
    });
});

缩短了几个文件以确定问题所在。在调用beforeEach(module('dir-templates'))时,它应该将所有匹配的.html文件加载到$ templateCache中,并防止抛出错误的GET请求。

任何帮助都会受到赞赏,因为它真的让我疯狂。如果您有任何其他问题,请发表评论。

3 个答案:

答案 0 :(得分:29)

所以,这似乎是一个两线修复的痛苦头痛。在Chrome中打开Karma(而不是PhantomJS)并查看源文件后,我注意到当ng-html2js将指令附加到$ templateCache时,它使用整个路径,而不是指令定义中提供的路径。

简而言之,'src/modules/test/test-directive.html.js' !== 'test-directive.html.js'

要实现此目的,请将karma.conf.js文件修改为ngHtml2JsProcessor,如下所示:

ngHtml2JsPreprocessor: {
    stripPrefix: 'src/',
    moduleName: 'dir-templates'
},

指令声明的templateUrl看起来像:

templateUrl: 'modules/test/test-directive.html'

答案 1 :(得分:0)

要添加有关确保匹配的模板名称与前缀匹配的注释(没有前导斜杠等),还要检查另一件事情。

模板缓存键区分大小写,因此请确保您的指令使用适当的大小写引用html文件。 ngHtml2JsPreprocessor生成的模板缓存键使用文件系统上实际文件名和目录名的大小写。

因此,如果您的文件名为Test-Directive.html,或者您的文件夹名为&#34; Modules&#34;但是你的指令引用了&#34; modules / test-directive.html&#34;,它不会从缓存中解析。

区分大小写不是指令的模板的实际(非测试)使用问题(GET请求显然不区分大小写,模板缓存密钥将根据初始GET生成请求是,即你的指令中指定的任何内容)。

答案 2 :(得分:0)

收到此错误时的一些调试提示:

当Karma chrome启动器打开浏览器时,搜索引发​​错误的模板,在我的情况下这是student-survey.html,您会看到student-survey.html.js这是放置在角度模板中的模板缓存。检查html.js文件,您将看到缓存ID: $templateCache.put('/app/views/student-survey.html',....

你必须确保缓存ID,在这种情况下app/views/student-survey.html将匹配你的应用程序中引用的内容,在我的情况下,缓存ID缺少一个尾随'/',我设法通过放置一个/prependPrefix部分ngHtml2JsPreprocessor部分的karma.conf.js

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