0

A) 단위 테스트를 수행하고 B) Angular 1.5.x의 구성 요소 기반 개발에 익숙해지기 위해 Angular 1.5에 대한 기본 단위 테스트를 작성하려고합니다. 나는 간단한 구성 요소 단위 테스트에 노력하고있어,하지만 난 다음과 같은 메시지가 계속 :단위 테스트 ngComponentRouter - 각도 1.5.x

Error: [$injector:modulerr] Failed to instantiate module app due to:

Error: [$injector:modulerr] Failed to instantiate module ngComponentRouter due to:

Error: [$injector:nomod] Module 'ngComponentRouter' 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.

내가 단위 테스트에이 특정 의존성/모듈을 삽입하는 방법에 대한 지침을 거의 싶습니다.

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

당신을 테스트에 컴포넌트 라우터를로드하지 마십시오. 유닛 테스트는 특정 유닛 (자신의 코드)을 독립적으로 테스트하기위한 것이기 때문에 * unit * 테스트에서 실제 라우터를 사용하면 안됩니다. 모의 $ 라우터 서비스. Btw, 구성 요소 라우터가 사용되지 않습니다. – estus

답변

0

그래서 두 가지 변화는 속임수를 썼는지 처음 내가 구성 요소 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 
    }); 
};