web-dev-qa-db-ja.com

Angular 2 + ng test: 'X'は既知のコンポーネントではありません

Angular 2.0 RTM with Angular CLI 1.0.0-beta 16:で新しいプロジェクトを作成しました

ng init

そして実行しました:

ng generate component my-new-component

そして実行しました:

ng test

そしてこれを手に入れました:

'app-my-new-component' is not a known element:
1. If 'app-my-new-component' is an Angular component, then verify that it is part of this module.
2. If 'app-my-new-component' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("
</h1>
<p>The mean of life is {{answer}}</p>
[ERROR ->]<app-my-new-component></app-my-new-component>"): AppComponent@4:0
Error: Template parse errors:
    at TemplateParser.parse (http://localhost:9877/_karma_webpack_/0.bundle.js:8669:19)
    at RuntimeCompiler._compileTemplate (http://localhost:9877/_karma_webpack_/0.bundle.js:17854:51)
    at http://localhost:9877/_karma_webpack_/0.bundle.js:17777:83
    at Set.forEach (native)
    at compile (http://localhost:9877/_karma_webpack_/0.bundle.js:17777:47)
    at RuntimeCompiler._compileComponents (http://localhost:9877/_karma_webpack_/0.bundle.js:17779:13)
    at RuntimeCompiler._compileModuleAndAllComponents (http://localhost:9877/_karma_webpack_/0.bundle.js:17696:37)
    at RuntimeCompiler.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/0.bundle.js:17684:21)
    at TestingCompilerImpl.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/0.bundle.js:24662:35)
    at TestBed._initIfNeeded (webpack:///home/adam/monctonug/~/@angular/core/bundles/core-testing.umd.js:1059:0 <- src/test.ts:4568:40)

コンポーネントを並べ替えてみましたが、役に立ちませんでした。この問題を解決する正しい方法は何ですか?

20
Adam Greene

私は同じ問題に直面していましたが、テストでは使用済みコンポーネントも宣言する必要があることがわかりました。

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent,
        MyNewComponent       
      ],
    });
33
ajurasz