Last active
June 9, 2017 05:36
-
-
Save sekky0905/f4e943799f332d7a873f765488ef912b to your computer and use it in GitHub Desktop.
Angular4(Angular2~)のユニットテスト【Angularのユニットテストの基本とComponentの簡単なテスト編】 ref: http://qiita.com/Sekky0905/items/13e71fce4f0a3cffbc09
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1> | |
{{title}} | |
</h1> | |
<p class="button" (click)="changeH1Element()">ここをクリックして</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {ComponentFixture, TestBed, async, ComponentFixtureAutoDetect,} from '@angular/core/testing'; | |
import {By} from '@angular/platform-browser'; | |
import {DebugElement} from '@angular/core'; | |
import {AppComponent} from './app.component'; | |
// describeでテストSuiteを作成 | |
describe('AppComponentのテスト', () => { | |
// テストの中のAppComponent | |
let comp: AppComponent; | |
// ComponentFixtureは、 componentのインスタンスそのものとcomponentのDOM elementのハンドルであるDebugElementへのアクセスを提供する。 | |
let fixture: ComponentFixture<AppComponent>; | |
// ComponentのDOM elementのhandle | |
let de: DebugElement; | |
let el: HTMLElement; | |
// 各Spec(個々のテスト)が開始される前に行う処理を設定する。 | |
// 非同期処理 | |
// Componentのインスタンスを生成する前に、Angular template compilerが外部ファイルを読み込む | |
beforeEach(async(() => { | |
// テストしたいクラスのためのモジュール環境をconfigureTestingModuleメソッドで設定する。 | |
// メタデータの登録 | |
TestBed.configureTestingModule({ | |
// テストされるComponentを登録 | |
declarations: [ | |
AppComponent | |
], | |
providers: [ | |
{provide: ComponentFixtureAutoDetect, useValue: true} | |
] | |
}).compileComponents(); // 外部ファイルをコンパイル | |
})); | |
// 同期処理 | |
// Componentのインスタンスを生成 | |
beforeEach(() => { | |
// ComponentFixtureは、 componentのインスタンスそのものとcomponentのDOM elementのハンドルであるDebugElementへのアクセスを提供する。 | |
fixture = TestBed.createComponent(AppComponent); | |
// テストされるComponentのインスタンス | |
comp = fixture.componentInstance; | |
// queryは、fixtureのDOM全体の中から、引数で与えたelementを満たす最初のDom elementとマッチしたDebugElementを返す | |
// Byでは、cssのselectorを生成している。 | |
de = fixture.debugElement.query(By.css('h1')); | |
el = de.nativeElement; | |
}); | |
// itでSpecを作成 | |
it('AppComponentのインスタンスが生成できているかどうか', async(() => { | |
// trueかどうかs | |
expect(comp).toBeTruthy(); | |
})); | |
it('何もしない場合のタイトルがAppComponentのtitleと同じかどうか', async(() => { | |
expect(el.textContent).toContain(comp.title); | |
})); | |
it('何もしない場合のタイトルがAppComponentのtitleと同じかどうか(上のテストと同じことをしている)', async(() => { | |
expect(el.textContent).toContain('テストだよ'); | |
})); | |
it('detectChangesが1回起きた時、h1の値が変更されるかどうか', async(() => { | |
comp.title = 'クリックされたぜ!'; | |
fixture.detectChanges(); | |
expect(el.textContent).toContain(comp.title); | |
})); | |
it('changeH1Elementが呼び出されたら、titleが変更されるかどうか', async(()=> { | |
comp.changeH1Element(); | |
expect(comp.title).toBe('クリックされたぜ!'); | |
})); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from '@angular/core'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent { | |
title = 'テストだよ'; | |
/** | |
* h1の文章を変更する | |
*/ | |
changeH1Element() { | |
this.title = 'クリックされたぜ!'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ng test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment