Angularjs 使用Jasmine测试Angular服务:未能实例化模块应用程序

Angularjs 使用Jasmine测试Angular服务:未能实例化模块应用程序,angularjs,jasmine,karma-runner,karma-jasmine,Angularjs,Jasmine,Karma Runner,Karma Jasmine,我正在尝试使用Jasmine为角度服务创建一个简单的单元测试。 但是当我通过Karma运行测试时,我得到了以下错误: Failed to instantiate module app 我不知道为什么我会犯这个错误。我在谷歌上搜索并尝试应用以下解决方案。但都不管用 我尝试更改karma.conf.js中文件的顺序 我试着用不同的方式编写单元测试。我的意思是以不同的方式注入服务 但错误依然存在 这是一个非常简单的代码。请查找以下代码以供参考: string.service.js (functi

我正在尝试使用Jasmine为角度服务创建一个简单的单元测试。 但是当我通过Karma运行测试时,我得到了以下错误:

Failed to instantiate module app
我不知道为什么我会犯这个错误。我在谷歌上搜索并尝试应用以下解决方案。但都不管用

  • 我尝试更改karma.conf.js中文件的顺序
  • 我试着用不同的方式编写单元测试。我的意思是以不同的方式注入服务
但错误依然存在

这是一个非常简单的代码。请查找以下代码以供参考:

string.service.js

(function () {
    "use strict";

    angular
        .module('app')
        .factory("stringService", stringService)

    function stringService() {

        return {
            checkForAlphaNumeric: checkForAlphaNumeric,
        };

        function checkForAlphaNumeric(string) {
            var regex = /^[a-zA-Z0-9]+$/;
            return regex.test(string);
        }
    }
})();
describe("-----> String Service", function () {

    var stringService;
    beforeEach(function () {
        angular.mock.module('app');
        inject(function ($injector) {
            stringService = $injector.get('stringService');
        });
    });

    it("--> stringService should be Defined.", function () {
        expect(stringService).toBeDefined();
    });
});
// list of files / patterns to load in the browser
files: [
  //External
  './node_modules/angular/angular.js', // Angular Framework
  './node_modules/angular-mocks/angular-mocks.js', // Loads the Module for Tests
  './node_modules/@uirouter/angularjs/release/angular-ui-router.js', // UI-Router
  './node_modules/angular-route/angular-route.js',

  //Sub Modules
  './app/authentication/authentication.module.js',

  //Main Module
  './app/app.module.js', // Angular App

  //Controllers
  './app/authentication/login.controller.js',

  //Services and Factories
  './app/services/string.service.js',

  //Specs
  './app/licensing/licensing.controller.spec.js',
  './app/services/string.service.spec.js'
],
string.service.spec.js

(function () {
    "use strict";

    angular
        .module('app')
        .factory("stringService", stringService)

    function stringService() {

        return {
            checkForAlphaNumeric: checkForAlphaNumeric,
        };

        function checkForAlphaNumeric(string) {
            var regex = /^[a-zA-Z0-9]+$/;
            return regex.test(string);
        }
    }
})();
describe("-----> String Service", function () {

    var stringService;
    beforeEach(function () {
        angular.mock.module('app');
        inject(function ($injector) {
            stringService = $injector.get('stringService');
        });
    });

    it("--> stringService should be Defined.", function () {
        expect(stringService).toBeDefined();
    });
});
// list of files / patterns to load in the browser
files: [
  //External
  './node_modules/angular/angular.js', // Angular Framework
  './node_modules/angular-mocks/angular-mocks.js', // Loads the Module for Tests
  './node_modules/@uirouter/angularjs/release/angular-ui-router.js', // UI-Router
  './node_modules/angular-route/angular-route.js',

  //Sub Modules
  './app/authentication/authentication.module.js',

  //Main Module
  './app/app.module.js', // Angular App

  //Controllers
  './app/authentication/login.controller.js',

  //Services and Factories
  './app/services/string.service.js',

  //Specs
  './app/licensing/licensing.controller.spec.js',
  './app/services/string.service.spec.js'
],
karma.conf.js

(function () {
    "use strict";

    angular
        .module('app')
        .factory("stringService", stringService)

    function stringService() {

        return {
            checkForAlphaNumeric: checkForAlphaNumeric,
        };

        function checkForAlphaNumeric(string) {
            var regex = /^[a-zA-Z0-9]+$/;
            return regex.test(string);
        }
    }
})();
describe("-----> String Service", function () {

    var stringService;
    beforeEach(function () {
        angular.mock.module('app');
        inject(function ($injector) {
            stringService = $injector.get('stringService');
        });
    });

    it("--> stringService should be Defined.", function () {
        expect(stringService).toBeDefined();
    });
});
// list of files / patterns to load in the browser
files: [
  //External
  './node_modules/angular/angular.js', // Angular Framework
  './node_modules/angular-mocks/angular-mocks.js', // Loads the Module for Tests
  './node_modules/@uirouter/angularjs/release/angular-ui-router.js', // UI-Router
  './node_modules/angular-route/angular-route.js',

  //Sub Modules
  './app/authentication/authentication.module.js',

  //Main Module
  './app/app.module.js', // Angular App

  //Controllers
  './app/authentication/login.controller.js',

  //Services and Factories
  './app/services/string.service.js',

  //Specs
  './app/licensing/licensing.controller.spec.js',
  './app/services/string.service.spec.js'
],
  • 交换模块和子模块参考也不起作用
  • 在控制器和服务之前或之后放置模块和子模块引用也不起作用

任何帮助都将不胜感激。

我已经找出了我的场景中的错误。让我解释一下:

  • 我的应用程序模块有几个依赖项
  • 我的字符串服务应用程序模块的一部分
  • 现在,当您想要测试字符串服务时,您必须在karma.conf.js文件部分中引用所有(表示所有)的应用程序模块依赖项。即使是您不使用的服务
  • 在我的例子中,我没有引用qrcode依赖项,因为我没有在字符串服务中使用qrcode
  • 而这个缺失的参考正是问题的根源。因此,karma无法实例化模块app
希望这对别人有帮助