2017-06-01 7 views
1

template parse error親で子コンポーネントを使用する場合。親コンポーネントで子コンポーネントを使用中にエラーが発生しました - 角2ユニットテスト(KarmaJasmine)

私は2つのコンポーネントを持っています。

1. CardComponent 
    2. InfoComponent 

マイCardComponentのHTML

 <div class="footer"> 
     <full-info></full-info> 
    </div> 

CardComponentのspecファイル:

beforeEach(async(() => { 
TestBed.configureTestingModule({ 
    declarations: [InfoComponent, CardComponent ], 
    imports: [ 
     FormsModule, 
     MaterialModule.forRoot() 
    ], 
    schemas: [ NO_ERRORS_SCHEMA ], //Used CUSTOM_ERRORS_SCHEMA also 
}) 
.compileComponents(); 
    })); 

マイInfoComponent TSコード:

import { Component, Input } from '@angular/core'; 


    @Component({ 
      selector: 'full-info', 
      templateUrl: './full-info.component.html', 
      styleUrls: ['./full-info.component.css'] 
      }) 

    export class InfoComponent { 
    public fullInfo; 
    } 

InfoComponent仕様宣言:

 beforeEach(async(() => { 
TestBed.configureTestingModule({ 
    declarations: [ InfoComponent ], 
    imports: [ 
     FormsModule, 
     MaterialModule.forRoot() 
    ], 
    schemas: [ NO_ERRORS_SCHEMA ] 
}) 
.compileComponents(); 
    })); 

これらの2つのコンポーネントのテストケースを実行すると、が個別にになります。

私はその後、私はエラーを取得しています時にこれらのテストケースを実行しています:

'full-info' is not a known element: 
1. If 'full-info' is an Angular component, then verify that it is part of this module. 
2. If 'full-info' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. (" 
    </div> 
    <div class="footer"> 
    [ERROR ->]<full-info></full-info> 
    </div> 

私も、私はこのエラーを取得していますCardComponentInfoComponentを輸入しています。私は問題を見つけることができません。私は誰にでもそれが私にとって素晴らしいことを知っています。

+0

「CardComponent」と「InfoComponent」を個別にテストすることができます。つまり、 'InfoComponent'ユニット内で' InfoComponent'が意図したとおりに動作していることをテストします。次に、 'CardComponent'テストで、' CardComponent'が意図したとおりに動作していることを確認します。なぜそれらを一緒にテストするのですか?つまり、 'CardComponent'をテストしているときに' InfoComponent'を既にテストしたはずですよね?したがって、それぞれの単体テストが合格すれば、どちらも正しいでしょうか? – SrAxi

+0

@SrAxi個別にテストすることは、それぞれに 'fdescribe'を使ってテストすることを意味します。 'fdescribe'を削除すると、エラーが発生します。 – Dalvik

答えて

0

両方のコンポーネントがまったく同じモジュールを共有していますか? モジュールは正常にインポートされていますか? forRoot()はまさに必要ですか? documentationから

Call forRoot only in the root application module, AppModule. Calling it in any other module, particularly in a lazy-loaded module, is contrary to the intent and can produce a runtime error.

Remember to import the result; don't add it to any other @NgModule list.

forRoot()を削除してみて、エラーが続くかどうかを確認。

希望私は助けてくれました、どうすればいいのか教えてください!

関連する問題