私はAngular 2 Frontendをテストしていますが、私はサイト(https://kendaleiv.com/angular-2-component-testing-template-using-testbed/)でng-test(角コアテスト)TestBedを使っている人を見つけました。これは本当に基本的な例です。 。Angular 2 Appテスト、html要素にアクセスして操作する方法は?
私のアプリケーションには入力フォーム要素があり、入力信号がフォーム要素から送信されると、それはInfoBox要素に結果セットが表示されます。 私がしたいのは、TestBedから要素フォームを取得し、それを検索して入力ボックスからの応答を取得するStringを提供することです。 (テスト3)
1)要素(フォームとインフォボックス)を取得するにはどうすればよいですか?
2)インフォボックスから信号を処理するにはどうすればよいですか?
3)シグナルが処理されたら、インフォボックスエレメントから結果セットを取得するにはどうすればよいですか?
私が使用しているコードサンプルは、 'ng test'コマンドで実行してChromeを起動し、最初の2つのテストが動作しています。
describe('AppComponent',() => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AgsResultsComponent,
AppComponent,
FormularComponent,
FormularAutocompleteComponent,
InfoBoxComponent,
InfoBoxAgencyDetailsComponent,
InfoBoxAgencyItemComponent
],
providers: [ConfigurationService],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
// Test 1
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
console.log(app);
expect(app).toBeTruthy();
}));
// Test 2
it('should render agsheading in a h1 tag', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Agentursuche');
}));
// Test 3
it('should execute a search using Form', async(() => {
const formular = TestBed.createComponent(FormularComponent);
const infoBox = TestBed.createComponent(InfoBoxComponent);
formular.detectChanges();
infoBox.detectChanges();
const formularCompiled = formular.debugElement.nativeElement;
const infoBoxCompiled = infoBox.debugElement.nativeElement;
formularCompiled.sendInput('Just a Test');
expect(infoBoxCompiled).toContain(something);
}));
});
ありがとうございました。私は、各サイクルをどこに置くべきか、さまざまな 'it()'テストをどのように編成するのかの例を教えてください。ありがとうたくさん – mattobob
テストを構成することは本当に挑戦です。これを助けることの1つは、さまざまなオープンソースプロジェクトを見て、どのように行っているかを見ることです。いくつかの例は[角度そのもの](https://github.com/angular/angular)、[素材](https://github.com/angular/material2)などです。次に、あなたのために働くことができるものについていくつかのアイデアがあります。 –
「beforeEach」サイクルはどうですか? – mattobob