使用karma html2js预处理器运行angularjs单元测试时无法实例化模块错误

时间:2014-10-29 18:32:14

标签: javascript angularjs unit-testing karma-jasmine

我正在尝试使用业力预处理器来获取单元测试中加载的模板。

我的angularjs单元测试指令是抛出'无法实例化模块错误'在运行测试时。我在karma.conf.js中使用karma html2js预处理器来获取指令单元测试中的模板。

karma.conf.js

config.set({
        preprocessors: {
            'Components/navBar/Templates/navBar.htm': ['ng-html2js']
        },

    files: [
    'Scripts/angular-mocks.js',
    ...
    ...
    'Client/Components/navBar/Templates/navBar.htm'
    ],
    ngHtml2JsPreprocessor: {
        moduleName: 'templates'
    }

navBar_testcase.js

describe("navbartesting", 
function () {
    var template;

beforeEach(module('templates'));

beforeEach(function () {
    module("navBar");
    inject(function ($rootScope, $compile, $httpBackend, $injector, $localStorage, $state, $templateCache, userSession, appConfig) {

        http = $httpBackend;
        $httpBackend = $injector.get('$httpBackend');
        ...
        ....
    });
});
});

运行上述单元时测试错误'无法实例化模块'''被扔了。这可能是什么问题?我们是否有任何物理路径可以定义为具有模板' karma配置文件中的模块?

1 个答案:

答案 0 :(得分:0)

使用此配置,可以制作模板。缺点是它只会创建view.tpl.html而不是test.tpl.html

 config.set({
        autoWatch: true,
        basePath: '../',
        browsers: ['Chrome'],
        frameworks: ['jasmine', 'requirejs'],

        reporters: ['progress'],

        plugins: [
            'karma-chrome-launcher',
            'karma-phantomjs-launcher',
            'karma-jasmine',
            'karma-requirejs',
            'karma-ng-html2js-preprocessor-requirejs'
        ],

        files: [
            // App-specific Code
            {pattern: 'src/**/*.js', included: false},

            // 3rd party code
            {pattern: 'lib/**/*.js', included: false},

            //test files
            {pattern: 'test/unit/**/*.js', included: false},

            'test/test-main.js',
            '**/test.tpl.html',
            '**/view.tpl.html'
        ],
        ngHtml2JsPreprocessor: {
            enableRequireJs: true,
            moduleName: 'templates',
            cacheIdFromPath: function (filepath) {
                console.log('path=' + filepath.replace(/^.*[\\\/]/, ''));
                return filepath.replace(/^.*[\\\/]/, '');
            },
        },

        exclude: [
            'src/main.js'
        ],
        preprocessors: {
            '**/*.html': ['ng-html2js']
    }