Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Angularで簡単な画面を単体テストまで一通り作った件
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
ふるてつ
September 11, 2019
Programming
1.4k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Angularで簡単な画面を単体テストまで一通り作った件
ふるてつ
September 11, 2019
More Decks by ふるてつ
See All by ふるてつ
ユニットテストとE2Eテストをdemoしたい件
furutetsu
0
280
はじめてのSpringBoot
furutetsu
0
500
AngularとBootstrapを使った業務システムの設計について考えた。
furutetsu
1
2.4k
Other Decks in Programming
See All in Programming
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
3.9k
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
0
180
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
340
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
130
Agentic UI
manfredsteyer
PRO
0
120
Oxlintのカスタムルールの現況
syumai
6
1k
LLM Plugin for Node-REDの利用方法と開発について
404background
0
160
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
180
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
380
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.2k
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
510
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
Featured
See All Featured
The Language of Interfaces
destraynor
162
27k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
Faster Mobile Websites
deanohume
310
31k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
570
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
AI: The stuff that nobody shows you
jnunemaker
PRO
8
700
A Modern Web Designer's Workflow
chriscoyier
698
190k
The Curse of the Amulet
leimatthew05
1
13k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
300
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Done Done
chrislema
186
16k
sira's awesome portfolio website redesign presentation
elsirapls
0
270
Transcript
Angularで簡単な画面を 単体テストまで一通り作った件 Angular もくもく会 in Fukuoka #9 2019.9.11
自己紹介 福岡のエンジニアです。 客先常駐するタイプです。 COBOL → VB6.0 → VB.net → Spring
BootなIT人生。 他 jQuery → AngularJS → Angular 定年後の夢は、年金をもらいながら自称IT発明家になることです。 ふるてつ@tetsufuru19681 https://tetsufuru.hatenablog.com/ ちょくちょく参加するコミュニティ
今日のおはなし Angular Materialを使って簡単なマスタメンテ画面を作りました。 会社マスタメンテ画面とサービス 軽く動いたところで初テストコードを書きました。 Jasmineでサービス1つ、コンポーネント1つ 目新しいことは特にありませんが、せっかくなのでご紹介します。 ※HTML、CSSはまだあまりちゃんとしていません。
すこし実物を見ていただきます。
CompanyListComponent 検索条件:会社名、会社名カナ、削除フラグ ボタン:新規、クリア、検索 一覧:ページネーションあり、一覧をクリックすると詳細へジャンプ DIしているもの CompanyService(自前) FormBuilder Title Router
CompanyService メソッド getCompanyList(httpParams: HttpParams) getCompany(companySeq: number) createCompany(companyDto: CompanyDto) updateCompany(companyDto: CompanyDto)
DIしているもの HttpClient TranslateService ErrorMessageService(自前)
company.service.spec.ts(デフォルト) import { TestBed, inject } from '@angular/core/testing'; import {
CompanyService } from '../company/company.service'; xdescribe('CompanyService', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [CompanyService] }); }); it('should be created', inject([CompanyService], (service: CompanyService) => { expect(service).toBeTruthy(); })); });
company.service.spec.ts describe('CompanyService', () => { let companyService: CompanyService; let httpClientSpy:
{ get: jasmine.Spy, post: jasmine.Spy, put: jasmine.Spy }; let translateServiceSpy: { instant: jasmine.Spy }; beforeEach(() => { httpClientSpy = jasmine.createSpyObj('HttpClient', ['get', 'post', 'put']); translateServiceSpy = jasmine.createSpyObj('TranslateService', ['instant']); TestBed.configureTestingModule({ providers: [ CompanyService, ErrorMessageService, { provide: HttpClient, useValue: httpClientSpy }, { provide: TranslateService, useValue: translateServiceSpy },], }); companyService = TestBed.get(CompanyService); }); 追加 追加 追加
company.service.spec.ts it('#getCompanyList:should return expected SearchCompanyListDto (HttpClient called once)', () =>
{ const expectedSearchCompanyListDto: SearchCompanyListDto = new SearchCompanyListDto(); const searchCompanyDto: SearchCompanyDto[] = [{companySeq: BigInt('1'), companyName: 'companyName', ~ 中略 ~ updateUser: 'updateUser', updateTime: new Date}]; expectedSearchCompanyListDto.searchCompanyDtos = searchCompanyDto; httpClientSpy.get.and.returnValue(asyncData(expectedSearchCompanyListDto)); companyService.getCompanyList(null).subscribe( searchCompanyList => expect(searchCompanyList).toEqual(expectedSearchCompanyListDto, 'expected searchCompanyDto'),fail); expect(httpClientSpy.get.calls.count()).toBe(1, 'one call'); }); DTO SPY
company-list.component.spec.ts TestBed.configureTestingModule({ declarations: [CompanyListComponent], schemas: [NO_ERRORS_SCHEMA], imports: [ReactiveFormsModule, BrowserAnimationsModule, MaterialModule,
HttpClientModule, TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: HttpLoaderFactory, deps: [HttpClient] } }), ], providers: [ FormBuilder, ~ 以下略 ~ 追加 追加
company-list.component.spec.ts it('should entry company name', () => { const debugElement:
DebugElement = fixture.debugElement; const queriedElement = debugElement.query(By.css('#companyName')); const htmlInputElement: HTMLInputElement = queriedElement.nativeElement; const expectedEntry = 'abcd1234日本語'; htmlInputElement.value = expectedEntry; htmlInputElement.dispatchEvent(new Event('input')); expect(component.companyName.value).toEqual(expectedEntry); }); css selector event
テストした内容 company.service 4つのメソッド(getListXX、getXX、updateXX、createXX)×2(正常/エラー2通り) company-list.component TypeScript 「新規」「クリア」「検索」ボタンから呼ばれるメソッドのテスト 「結果一覧」リストをクリックした時に呼ばれるメソッドのテスト DOM 検索条件の項目名 検索条件の初期値
検索条件の入力値 検索用のhttpパラメータ値
None
None
課題・検討 無駄にspyを作った気がする。 [NO_ERRORS_SCHEMA]の使いどころをまだわかっていない。 DOMのテストの成果が良くわからない ⇒ DOMのカバレッジはない のか? Materialでidを追加したコードがブラウザで表示されると違うものに なる場合があった。 検索ボタンクリックのテストは
async ()=>でないと動かなかった。 DOMについて一覧のヘッダーやデータ行の内容を確認すべきか? 悩んだ。
参考サイト Material 環境:https://material.angular.io/guide/getting-started コントロール:https://material.angular.io/components/categories/forms 一覧関連:https://material.angular.io/components/categories/tables ページネーションのサンプル:https://material.angular.io/components/table/examples (上記URLの「Table retrieving data
through HTTP」にあります) テスト:https://angular.jp/guide/testing 上記URL内の「サービスのテスト」、「コンポーネントテストの基本」、「コンポーネントクラスのテスト」、「コンポーネント DOMのテスト」
わたしのブログ記事 Angular8 で Web アプリを作ろう - Jasmine - Serviceのテスト https://tetsufuru.hatenablog.com/entry/2019/08/13/130116
Angular8 で Web アプリを作ろう - Jasmine - Componentのテスト その1 https://tetsufuru.hatenablog.com/entry/2019/09/01/015709 Angular8 で Web アプリを作ろう - Jasmine - Componentのテスト その2 DOM https://tetsufuru.hatenablog.com/entry/2019/09/01/222413
ありがとうございました。