簡體   English   中英

單元測試ngComponentRouter-Angular 1.5.x

[英]Unit testing ngComponentRouter - Angular 1.5.x

我正在嘗試為Angular 1.5構建基本的單元測試,目的是A)練習單元測試,以及B)熟悉Angular 1.5.x中基於組件的開發。 我正在嘗試對一個簡單的組件進行單元測試,但是我不斷收到以下消息:

錯誤:[$ injector:modulerr]由於以下原因,無法實例化模塊應用程序:

錯誤:[$ injector:modulerr]由於以下原因,無法實例化模塊ngComponentRouter:

錯誤:[$ injector:nomod]模塊'ngComponentRouter'不可用! 您可能拼錯了模塊名稱,或者忘記了加載它。 如果注冊模塊,請確保將依賴項指定為第二個參數。

我想要一些有關如何將此特定依賴項/模塊注入單元測試的指南。 這是我的代碼:

app.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();
    }); 
});

因此,完成了兩項更改,首先,我需要更改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
  });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM