首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS业力测试:意外请求: GET.template.html

AngularJS业力测试:意外请求: GET.template.html
EN

Stack Overflow用户
提问于 2016-06-17 18:22:01
回答 2查看 2.4K关注 0票数 4

我正试图为我的AngularJS应用程序设置业力测试,但不知怎么的,我总是失败。我已经执行了所有步骤,这里,但我觉得ng-html2js预处理程序没有正常工作。我总是收到以下信息:

错误:意外请求:获取js/模板/my-template.template.html

这是我的代码结构

在这里我的测试结构

我试图测试的指令是:

代码语言:javascript
复制
angular
    .module('myapp')
    .directive('myDirective', ['myService', function (myService) {
        return {
            restrict: 'E',
            templateUrl: 'js/templates/my-template.template.html',
            scope: {},
            link: function (scope) {
            }
        }
    }]);

我的因果报应是这样的:

代码语言:javascript
复制
module.exports = function (config) {
config.set({

    basePath: '../../',

    frameworks: ['jasmine'],


    files: [
        //some omitted like angular and so on...
        'main/resources/static/**/*.js',
        '**/*.html',
        'test/js/**/*Spec.js'
    ],

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

    ngHtml2JsPreprocessor: {
        moduleName: 'templates'
    }
... (more default stuff omitted)
}

mySpec.js是这样的:

代码语言:javascript
复制
describe('directive', function () {
    'use strict';

    beforeEach(module('myapp'));
    beforeEach(module('templates'));

    var elm,
        scope;

    beforeEach(inject(function ($rootScope, $compile) {
        elm = angular.element(
        '<my-directive>' +
        '</my-directive>');

        scope = $rootScope.$new();

        $compile(elm)(scope);
        scope.$digest();
    }));

    describe('Does something', function () {
        console.log('test');
        expect(true).toBe(true);
    });
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-17 19:30:19

我想我明白了。解决方案在karma.conf.js中:

代码语言:javascript
复制
ngHtml2JsPreprocessor: {
    cacheIdFromPath: function (filepath) {
        //The suggested filepath.strip would through an error
        var cacheId = filepath.replace('main/resources/static/', '');
        return cacheId;
    },
    moduleName: 'templates'
}
票数 1
EN

Stack Overflow用户

发布于 2016-06-17 18:30:10

在beforeEach函数中为指令的html模板添加一个模拟模板。使用$templateCache

代码语言:javascript
复制
describe('directive', function () {
    'use strict';

    beforeEach(module('myapp'));
    beforeEach(module('templates'));

    var elm,
        scope;

    beforeEach(inject(function ($rootScope, $compile, $templateCache) {


        $templateCache.put('js/templates/my-template.template.html','HTML_OF_YOUR_DIRECTIVE');

        elm = angular.element(
        '<my-directive>' +
        '</my-directive>');

        scope = $rootScope.$new();

        $compile(elm)(scope);
        scope.$digest();
    }));

    describe('Does something', function () {
        console.log('test');
        expect(true).toBe(true);
    });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37888004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档