angularjs、因果报应和requirejs测试用例错误

angularjs, karma and requirejs test case error

本文关键字:测试用例 错误 requirejs 因果报应 angularjs      更新时间:2023-09-26

我的angularjs应用程序使用requirejs卡。根据这篇文章,我正在编写我的测试用例,但在执行测试用例时总是会出现这个错误

Error: [$injector:modulerr] Failed to instantiate module sampleapp due to:
Error: [$injector:nomod] Module 'sampleapp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

测试用例:

define([
    'angular',
    'angular-mocks'
], function() {
    describe('main controller todo app', function() {
        var scope, ctrl;
        beforeEach(module('sampleapp',[]));
        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new();
            ctrl = $controller('mainCtrl', {$scope: scope});
        }));
        it('is todo added', function() {
            scope.todo = 'My new Todo';
            scope.addTodo();
            assert.equal(scope.todos.length, 1);
        });
    })
});

因果报应:

module.exports = function(config) {
    'use strict';
    config.set({
        basePath: './',
        frameworks: [
            'requirejs',
            'mocha', 
            'chai'
        ],
        files: [{
            pattern: 'test/test-main.js',
            watched: true
        }, {
            pattern: 'app/angular/**/*.js', 
            included: false
        }, {
            pattern: 'app/*.js', 
            included: false
        }, {
            pattern: 'app/scripts/additional/**/*.js', 
            included: false
        }, {
            pattern: 'test/spec/**/*.js',
            included: false,
            watched: true
        }],
        exclude: [
            'app/main.js'
        ],
        reporters: ['spec'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        captureTimeout: 20000,
        singleRun: false,
        plugins: [
            'karma-chai', 
            'karma-mocha',
            'karma-requirejs', 
            'karma-spec-reporter', 
            'karma-chrome-launcher'
        ]
    });
}

主测试:

var tests = [];
var TEST_REGEXP = /test[A-Za-z0-9]{1,}-[A-Za-z0-9]{1,}'.js/i;
Object.keys(window.__karma__.files).forEach(function(file) {
    if (TEST_REGEXP.test(file)) {
        tests.push(file);
    }
});
requirejs.config({
    'baseUrl': '/base/app',
    'paths': {
        'angular': 'scripts/additional/angular/angular',
        'angular-mocks': 'scripts/additional/angular-mocks/angular-mocks',
    },
    'shim': {
       'angular-mocks' : {'exports' : 'angular-mocks', 'deps': ['angular']}
    },
    'deps': tests,
    'callback': window.__karma__.start
});

非常感谢任何帮助或指导来解决此问题

主要问题是没有加载创建模块的文件(即执行代码var app = angular.module("sampleapp",[]);的文件)

一旦纠正了这一点,一切都开始工作