0

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

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
});

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

4

1 回答 1

0

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

一旦纠正,一切都开始工作

于 2015-08-03T09:04:01.803 回答