为什么我得到一个'无法实例化模块应用程序'我的angularjs测试中的错误?

时间:2017-12-21 04:04:23

标签: javascript angularjs unit-testing

在我的角度js测试中,我收到此错误:

 Error: [$injector:modulerr] Failed to instantiate module app due to:
        Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to load it.

尝试为我的服务编写单元测试,它是这样开始的:

var $httpBackend, httpBasedService;

beforeEach(module('app'));

beforeEach(inject(function (_$httpBackend_, _httpBasedService_) {
    $httpBackend = _$httpBackend_;
    httpBasedService = _httpBasedService_;
}));

这是服务:

app.factory('httpBasedService', function($http) {
    return {
        getUsers: function() {
            return $http.get('bla')
                .then(function(result) {
                    return result.data;
                });
        }
    };
});

karma.conf.js:

//jshint strict: false
module.exports = function (config) {
    config.set({

        basePath: './app',

        files: [
            'bower_components/angular/angular.js',
            'bower_components/angular-route/angular-route.js',
            'bower_components/angular-mocks/angular-mocks.js',
            'components/**/*.js',
            'view*/**/*.js',
            'services/myService_test.js'
        ],

        autoWatch: true,

        frameworks: ['jasmine'],

        browsers: ['Chrome'],

        plugins: [
            'karma-chrome-launcher',
            'karma-firefox-launcher',
            'karma-jasmine',
            'karma-junit-reporter'
        ],

        junitReporter: {
            outputFile: 'test_out/unit.xml',
            suite: 'unit'
        }

    });
};

为什么我会收到此错误以及如何解决此问题? 这是github

的链接

1 个答案:

答案 0 :(得分:0)

您在应用中使用angular.module('myApp',...,如下所示

  // Declare app level module which depends on views, and components
  var app=angular.module('myApp', [

但在测试文件中使用其变量名称 试试这个

  beforeEach(module('myApp'));

而不是beforeEach(module('app'));

希望这对你有帮助!