私はトップレベルのコンポーネントAppComponent
を持っていて、それはtemplate
プロパティで、ページ上に<child>
要素をレンダリングします。 (<child>
は別の角度成分である)。親ユニットが角単位テストに存在することをアサートする
ユニットテストに<child>
要素を無視するか、何らかの方法でテスト用に宣言する必要がありますか?
私はこのテストファイルで実際に<child>
をテストしようとしていません。その機能を検証するために、別のユニットテストファイルを作成します。
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ChildComponent } from './child.component';
@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent, ChildComponent],
imports: [BrowserModule]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app',
template: '<child></child>'
})
export class AppComponent { }
child.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'child',
templateUrl: './child.component.html'
})
export class ChildComponent { }
app.component.spec.ts
import { async, TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { ChildComponent } from './child.component';
describe('App', (() => {
beforeEach(async() => {
TestBed.configureTestingModule({ declarations: [AppComponent, ChildComponent] }).compileComponents();
}));
it('should work',() => {
let fixture = TestBed.createComponent(AppComponent);
// assert app.component exists
});
});
私はユニットテストを実行すると、私はこのエラーを取得しています:
Error: This test module uses the component ChildComponent which is using a "templateUrl" or "styleUrls", but they were never compiled. Please call "TestBed.compileComponents" before your test. in /tmp/karma-typescript-bundle-3142O76A6m7KjQOD.js (line 3929)
私は '宣言'に 'ChildComponent'を追加してから' compileComponents() 'を呼び出しましたが、' compileComponents() 'が呼び出されていることを検出していません – bobbyrne01