0

我正在尝试为 Angular 1.5 构建基本单元测试,目的是 A) 练习单元测试,以及 B) 熟悉 Angular 1.5.x 中基于组件的开发。我正在尝试对一个简单的组件进行单元测试,但我不断收到以下消息:

错误:[$injector:modulerr] 无法实例化模块应用程序,原因是:

错误:[$injector:modulerr] 无法实例化模块 ngComponentRouter,原因如下:

错误:[$injector:nomod] 模块“ngComponentRouter”不可用!您要么拼错了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

我想要一些关于如何将这个特定的依赖项/模块注入到单元测试中的指导。这是我的代码:

应用程序.js

(function(){
    "use strict";

    const app = angular.module("app", ["ngComponentRouter"])

    app.value("$routerRootComponent", "componentApp");
})();

组件-app.component.js

(function(){
    "use strict";

    angular.module("app").component("componentApp", {
        templateUrl: "/javascripts/component-app.component.html",
        controller: ["$router", function($router){
            $router.config([
                { path: "/users", component: "usersComponent", name: "Users" },
                { path: "/**", redirectTo: ["Users"]}
            ]);
        }]

    });
})();

组件-app.component.spec.js

describe("componentApp component", function(){
    beforeEach(module("app"));

    var $componentController, componentApp;

    beforeEach(inject(function($injector){
        $componentController = $injector.get("$componentController");
        componentApp = $componentController("componentApp", { $scope: {}});
    }));

    it("componentApp component is defined", function(){
        expect(componentApp).toBeDefined();
    }); 
});
4

1 回答 1

0

所以两个改变就成功了,首先我需要改变component-app.component.js:

angular.module("app").component("componentApp", {
    templateUrl: "/templates/component-app.component.html",
    $routeConfig: [
        { path: "/Users", name: "Users", component: "usersComponent" },
        { path: "/Home", name: "Home", component: "homeComponent"},
        { path: "/Profile/:id", name: "Profile", component: "profileComponent" },
        { path: "/**", redirectTo: ["Users"]}
    ]
});

我需要在 karma.conf.js 中包含该文件:

module.exports = function(config) {
  config.set({

    basePath: "",
    frameworks: ["jasmine"],
    files: [
        "bower_components/angular/angular.js",       
        "bower_components/angular-mocks/angular-mocks.js",
        "bower_components/angular-component-router/angular_1_router.js",
        "javascripts/**/*.js"
    ],
    exclude: [        
    ],
    preprocessors: {
    },
    reporters: ["progress"],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ["Chrome"],
    singleRun: false,
    concurrency: Infinity
  });
};
于 2017-03-27T23:16:29.943 回答