簡體   English   中英

使用Karma html2js預處理器運行angularjs單元測試時無法實例化模塊錯誤

[英]Failed to instantiate module error while running angularjs unit tests using karma html2js preprocessor

我正在嘗試使用業力預處理程序以將模板加載到單元測試中。

我的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');
        ...
        ....
    });
});
});

在運行上述單元測試時,將引發錯誤“無法實例化模塊”模板。 這可能是什么問題? 我們是否需要定義任何物理路徑以在業力配置文件中具有“模板”模塊?

使用此配置,可以制作模板。 缺點是它將僅創建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']
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM