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
FactoryBot the Right Way
Search
toshimaru
October 03, 2020
Technology
12
4.9k
FactoryBot the Right Way
Kaigi on Rails (
https://kaigionrails.org
)の登壇資料です。
toshimaru
October 03, 2020
Tweet
Share
More Decks by toshimaru
See All by toshimaru
OKR基本のキ / OKR Basics
toshimaru
3
2.5k
RubyKaigiのプロポーザルを通したい。 / rubykaigi-proposal
toshimaru
3
3.2k
10年モノのレガシーPHPアプリケーションを移植しきるまでの泥臭くも長い軌跡 / legacy-php-app-migration
toshimaru
1
2.3k
Faster Pull Request Reviews 〜ハイパフォーマンスチームへの道〜 / Faster Pull Request Reviews
toshimaru
7
2.6k
medpeer.jp 開発を加速させるエンジニアリング施策 / medpeer engineering kaigi on rails 2021
toshimaru
2
6k
アラサーエンジニアの生存戦略 / career strategy for around thirty engineer
toshimaru
43
21k
Fat Modelの倒し方 / how to deal with fat model
toshimaru
20
19k
Make Rails Documents SEO(Search Engine Optimized)
toshimaru
3
1.6k
ActiveRecordデータ処理アンチパターン / active-record-anti-patterns
toshimaru
31
20k
Other Decks in Technology
See All in Technology
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
100
Jetpack Composeで始めるServer Cache State
ogaclejapan
2
170
サイバー攻撃を想定したセキュリティガイドライン 策定とASM及びCNAPPの活用方法
syoshie
3
1.2k
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
1
290
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
220
Microsoft Azure全冠になってみた ~アレを使い倒した者が試験を制す!?~/Obtained all Microsoft Azure certifications Those who use "that" to the full will win the exam! ?
yuj1osm
2
110
アップデート紹介:AWS Data Transfer Terminal
stknohg
PRO
0
180
PHP ユーザのための OpenTelemetry 入門 / phpcon2024-opentelemetry
shin1x1
1
160
サーバレスアプリ開発者向けアップデートをキャッチアップしてきた #AWSreInvent #regrowth_fuk
drumnistnakano
0
190
KubeCon NA 2024 Recap: How to Move from Ingress to Gateway API with Minimal Hassle
ysakotch
0
200
ハイテク休憩
sat
PRO
2
140
TSKaigi 2024 の登壇から広がったコミュニティ活動について
tsukuha
0
160
Featured
See All Featured
Making Projects Easy
brettharned
116
5.9k
BBQ
matthewcrist
85
9.4k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
Navigating Team Friction
lara
183
15k
Designing Experiences People Love
moore
138
23k
Making the Leap to Tech Lead
cromwellryan
133
9k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
It's Worth the Effort
3n
183
28k
How to Ace a Technical Interview
jacobian
276
23k
Fireside Chat
paigeccino
34
3.1k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Transcript
FactoryBot The Right Way 2020.10.3 Kaigi on Rails
Self Introduction • GitHub: @toshimaru • Twitter: @toshimaru_e • Software
Engineer at MedPeer Inc. • ࠷ۙͷٕज़తؔ৺ • Vue.js, TypeScript, Ruby3ͷܕγεςϜ
࠾༻ใ https://medpeer.co.jp/recruit/ ։ൃϒϩά https://tech.medpeer.co.jp/ 8FSF)JSJOH
FactoryBot The Right Way ࠓςετσʔλੜϥΠϒϥϦɺfactory_bot ͷਖ਼͍͍͠ํ ʢRight Wayʣʹ͍͓ͭͯ͠·͢ɻ
Why? • ͜Ε·Ͱʮਖ਼͍͠RailsΞϓϦέʔγϣϯͷॻ͖ํʯʮਖ਼͍͠RSpec ςετͷॻ͖ํʯଟ͘հ͞Ε͖ͯͨɻ • Ұํɺʮਖ਼͍͠ςετσʔλͷ࡞Γํʯ͋·Γٞ͞Εͯ͜ͳ͔ͬ ͨΑ͏ʹࢥ͑Δɻ
Rails ඪ४తςετߏ ϥΠϒϥϦ ςετର 5FTUJOH'SBNFXPSL 34QFD NJOJUFTU 3BJMT"QQMJDBUJPO .PEFM
$POUSPMMFS 7JFX 'JYUVSF 'BDUPSZ#PU 3BJMT'JYUVSFT .PEFM0CKFDU %BUBCBTF
Why Fixture Matters • ςετͷલఏͱͳΔঢ়ଶΛ࡞Γग़͢ͷ͕ Fixture • લఏ݅Λؒҧ͑ɺͦͷޙͷςετഁ͢Δ • Fixture
Ͱਖ਼͍͠લఏঢ়ଶΛ࡞Γग़͢͜ͱ͕ॏཁ • ςετຖʹԿ܁Γฦ͠ݺΕΔ • ඇޮͳσʔλੜΛ͍ͯͨ͠߹ɺςετΛͤ͘͞ΔཁҼʹ
͢͜ͱ 1. FactoryBot: The Right Way / The Good Way
• Right Way: ެࣜυΩϡϝϯτ͔ΒͷൈਮɾҾ༻ • Good Way: Right Way ͔Βநग़ͨ͠άουϓϥΫςΟε 2. FactoryBot: The Wrong Way (Bad Way) • FactoryBotͷޡ͍ͬͨํɾྑ͘ͳ͍͍ํ
ൃදͷߏ ✅ The Right Way The Good Way The Wrong
Way Կ͕μϝʁ/Ͳ͏͢Δʁ
ϕϯνϚʔΫʹ͍ͭͯ • ϕϯνϚʔΫڥ • factory_bot v6.1.0 / Ruby v2.7 •
Database sqlite3 Λ༻ (In-Memory DB, File DB ͷ2ͭΛݕূ) • GitHub Actions ্ͰϕϯνΛΒͤΔ • benchmark-ips Ͱൺֱ • Repository: toshimaru/factory-bot-the-right-way
✅ The Right Way factory_bot gemಋೖ Rails ͷ߹:
✅ The Right Way factory_bot methodಋೖ Ϋϥε໊Λলུͯ͠FactoryੜϝιουΛݺͼग़͢͜ͱ͕Ͱ͖ΔΑ͏ʹ:
✅ The Right Way Factoryఆٛ
The Wrong Way ڊେσϑΥϧτσʔλ
The Wrong Way ڊେσϑΥϧτσʔλ: Կ͕μϝʁ • ຊʹͦͷσʔλɺશ෦ඞཁͰ͔͢ʁ • ͦΕΛར༻͢ΔશͯͷςετέʔεͰ࠷খݶͷલఏঢ়ଶͷੜʹͳͬ ͍ͯΔ͔ʁ
• ແବͳσʔλੜʹͳ͍ͬͯͳ͍͔ʁ • ແବͳσʔλੜ • ؒҧͬͨલఏঢ়ଶΛ࡞Γग़͍ͯ͠Δ • ύϑΥʔϚϯεѱԽ
The Wrong Way ڊେσϑΥϧτσʔλ: Ͳ͏͢Δʁ • ࠷খݶͷσʔληοτఆٛΛσϑΥϧτFactoryͱͯ͠ఆٛ͠·͠ΐ ͏ʢref. Defining factories
> Best practicesʣ
✅ The Right Way Factory ϑΝΠϧஔ • Լهύεʹఆٛ͞ΕͨFactoryϑΝΠϧࣗಈతʹϩʔυ͞ΕΔ • test/factories.rb
• spec/factories.rb • test/factories"/*.rb • spec/factories"/*.rb
The Good Way Factory ϑΝΠϧஔ
✅ The Right Way Factory ੜ
The Wrong Way ແବcreate • build ͰࡁΉͱ͜ΖΛͳΜͰ͔ΜͰ create Λ࣮ͬͯϨίʔυΛ ੜͯ͠͠·͏͜ͱ
The Wrong Way ແବcreate: Կ͕μϝʁ • Ϣχοτςετͷඞཁ࠷ݶͳલఏ݅ʹͳ͍ͬͯͳ͍ • DBʹσʔλ͕ create
͞Ε͍ͯΔ͜ͱඞཁ݅Ͱͳ͍ • όϦσʔγϣϯͷςετͷ߹ɺvalid/invalidͳRubyΦϒδΣΫτͷঢ়ଶ ͑͞࡞͍ͬͯΕΑ͍ • SQL͕ΔɺύϑΥʔϚϯεѱԽ • ςετ୯ମͰʹͳΔ͜ͱ͕ͳ͍͕ɺνϦπϞͰςετΛͤ͘͞Δ ཁҼʹͳΔ
The Wrong Way ແବcreate: Կ͕μϝʁ • create, build ͦΕͧΕΛͬͨ߹ͷSQLൺֱɿ
The Wrong Way ແବcreate: Ͳ͏͢Δʁ • buildΛ͍·͠ΐ͏
The Wrong Way ແବcreate: ϕϯνϚʔΫ • In-Memory DB • File
DB
The Good Way build_stubbed ׆༻ • Լهͷ߹ͷςετ • ʮcreated_at
build ͡Όੜ͞Εͳ͍ͧ ʯ
The Good Way build_stubbed ׆༻ • build_stubbed Λ׆༻͢Εɺελϒ͞Εͨid, timestampͱͱʹΦ ϒδΣΫτ͕ੜ͞ΕΔ
✅ The Right Way Trait (1)
✅ The Right Way Trait (2)
The Wrong Way Too Many Traits • ྫ֎తͳɺڥքͳͲΛtrait ͰΕͳ͘શ෦ఆٛ͠Α͏ͱ͢ Δ͜ͱ
The Wrong Way Too Many Traits: Կ͕μϝʁ • traitఆٛ രൃ
• Fat Model ͳΒ͵ Fat Factory ͷग़དྷ্͕Γ • Fat Factory • ຊʹඞཁͳ trait ͕ݟ͚ͭʹ͘͘ͳΔ • ͳΜͰ͔ΜͰશͯͷύλʔϯΛ trait ʹدͤͯॻ͖ͨ͘ͳͬͪΌ ͏පؾ͕ൃ͢Δ
The Wrong Way Too Many Traits: Ͳ͏͢Δʁ • ͍ճͤΔ൚༻తͳৼΔ͍ͷΈΛ trait
ʹఆٛ͢Δ • ྫ֎తͳݸผͷςετʹدͤΔ • ྫ: ϞσϧͷόϦσʔγϣϯςετ
The Wrong Way DRY͡Όͳ͍traits • DRY (Don't repeat yourself) Ͱͳ͍
traitɺॏෳͨ͠ trait ͕ଟ͘ఆٛ͞Ε͍ͯ Δঢ়ଶͷ͜ͱ
The Wrong Way DRY͡Όͳ͍traits: Կ͕μϝʁ • traitఆٛ Έ߹Θͤരൃ • Fat
Model ͳΒ͵ Fat Factory ͷग़དྷ্͕Γʢref. Too Many Traitsʣ
The Wrong Way DRY͡Όͳ͍traits: Ͳ͏͢Δʁ • ಈ࡞͢ΔσϑΥϧτͷঢ়ଶΛ Factory ͱͯ͠γϯϓϧʹఆٛ͠ɺͦΕ ʹಛघͳঢ়ଶΛఆٛ͢ΔtraitΛॏͶ͍ͯ͘
The Wrong Way DRY͡Όͳ͍traits: Ͳ͏͢Δʁ •আ͞Ε͍ͯΔAdminϢʔβʔ࡞ ߏஙύλʔϯɿ
✅ The Right Way sequence • sequence Λ͏͜ͱͰσʔλʹγʔέϯγϟϧͳφϯόϦϯά͕Մ ೳʹ
The Wrong Way ಠࣗsequence id • DBͰ࠾൪͞ΕΔidΛಠࣗͷidͰΦʔόʔϥΠυͯ͠͠·͏͜ͱ
The Wrong Way ಠࣗsequence id: Կ͕μϝʁ • Ϩίʔυ࡞ͤͣͱid࠾൪͕ߦΘΕΔ • ࣮ࡍͷDBͷڍಈͱҟͳΔͷͰɺϦΞϧͳςετʹͳΒͳ͍
The Wrong Way ಠࣗsequence id: Ͳ͏͢Δʁ • ID࠾൪DBʹͤ·͠ΐ͏ • Stub͞Εͨ
id ͕ཉ͍͠ͷͰ͋Εɺ build_stubbed Λར༻͠·͠ΐ ͏ʢref. build_stubbed ׆༻ʣ
The Wrong Way ಉ͡ͷςετ
The Wrong Way ಉ͡ͷςετ: Կ͕μϝʁ • name ͕ಉ͡Λੜ͍ͯ͠ΔͷͰɺuser1.name ͕ user2.name
Ͱ user3.name Ͱςετ͕௨ͬͯ͠·͏
The Wrong Way ಉ͡ͷςετ: Ͳ͏͢Δʁ • sequence ΛͬͯϢχʔΫੑͷ͋ΔσʔλΛੜ͢Δ • ϦΞϧϫʔϧυʹ͍ۙσʔλΛ
faker Λͬͯੜ͢Δ
✅ The Right Way build_list, create_list • build_list: ࢦఆճ build
ͨ͠ίϨΫγϣϯΛฦ͢ • create_list: ࢦఆճ create ͨ͠ίϨΫγϣϯΛฦ͢
The Wrong Way ແବlistੜ • ෆඞཁʹ create_list ͯ͠ແବͳσʔλੜΛߦ͏͜ͱ
The Wrong Way ແବlistੜ: Կ͕μϝʁ • ͦͷσʔλɺશ෦ඞཁͰ͔͢ʁ (ref. ڊେσϑΥϧτσʔλ) •
ແବͳσʔλੜʹͳ͍ͬͯͳ͍͔ʁ
The Wrong Way ແବlistੜ: Ͳ͏͢Δʁ • transient + evaluator ͷػ
ೳΛ͍ؔ࿈ઌϨίʔυੜ Λίϯτϩʔϧ͢Δ • ຊʹඞཁͳ͚ͩࢦఆ͠ ͯϨίʔυੜ͢ΔΑ͏ʹ ͢Δ
✅ The Right Way Association ఆٛ ↓ ্ड़ͷ post Factory
Լهͱಉҙ ↓
The Wrong Way Always Create Association • association ઌΛ create
Ͱ࡞ͯ͠͠·͏͜ͱ
The Wrong Way Always Create Association: Կ͕μϝʁ • build ͨ͠߹Ͱৗʹ
createʢϨίʔυੜʣ͕͏
The Wrong Way Always Create Association: Ͳ͏͢Δʁ • ૉʹ association
ఆٛ͢Δ
The Wrong Way Always Create Association: ϕϯνϚʔΫ • In-Memory DB
• File DB
✅ The Right Way FactoryBot.lint • factory_bot ެ͕ࣜఏڙ͢ΔLintπʔϧ • FYI:
rubocop-rspec ʹ FactoryBot ༻ͷϧʔϧಉࠝ͞Ε͍ͯΔ • ref. RuboCop"::Cop"::RSpec"::FactoryBot
✅ The Right Way FactoryBot.lint: how it works • ఆٛ͞Εͨ
Factory ΛҰ௨Γ create ͯ͠ɺinvalid ͳ Factory ఆٛΛ ݕ͢Δ • traits: true ΦϓγϣϯͰ trait ؚΊͯݕࠪ͢Δ
• rakeλεΫԽͯ͠ଞLintπʔϧ ʢrubocopʣͱಉ༷ʹCIͷλε Ϋͷ̍ͭͱ͓ͯͤͯ͘͠ The Good Way FactoryBot.lint: rakeλεΫԽ
✅ FactoryBot The Right Way ·ͱΊ • ਖ਼͍͠લఏঢ়ଶΛਖ਼͘͠࡞Δ • ؆୯ʹσʔλఆٛɾϨίʔυੜͰ͖ͯ͠·͏ނʹɺ؆୯ʹແବσʔλ
Λ࡞Γग़͍͢͠ • ԼهΛҙࣝͯ͠ facotry_bot Λ͍͜ͳͦ͏ • Factory ͷσϑΥϧτఆٛϛχϚϧͳσʔληοτʹͳ͍ͬͯΔ͔ʁ • ֤ʑͷςετέʔεͰ͍ͬͯΔ Factory ࠷ݶͷલఏঢ়ଶΛ࡞Γग़ͤ ͍ͯΔ͔ʁ
Happy Testing Life!
Tips: FactoryGirl→FactoryBot • 2008 FactoryGirlੜ • 2017 FactoryGirl͔ΒFactoryBotʹϦωʔϜ • ref.
Project Naming History ⾠ OSS ͷ Naming ؾΛ͚ͭΑ͏ʂ
Tips: ςετσʔλͷϞοΫ/ελϒʹ͍ͭͯ “ϞοΫ/ελϒʹΑͬͯςετίʔυͷ࣮ߦεϐʔυ͕ૣ͘ͳΓςετ ͷੑߴ·ΔҰํɺϞοΫ/ελϒʻࣗͷໝɾئ͕࡞Γग़ ͨ͠ৼΔ͍ʼʹͳΔͷͰɺ࣮ࡍͷৼΔ͍ͱҟͳΔͱ͍͏ϦεΫ ΛሃΉɻ·ͨϞοΫ/ελϒΛ࡞Δ͜ͱͰɺϝϯςϯφϯεରͱͳΔ ίʔυͷྔɾॏෳ͕ଟ͘ͳΔɻ ϩʔΧϧڥͰ࠶ݱͰ͖ΔRDBKVSՄೳͳݶΓຊΛ͏ɻ” ʢfukabori.fm 13.
ϖΞϓϩςετͷٙͱ͔ɺιϑτΣΞΤϯδχΞͷҭͱ͔ ͷtwada ͞ΜͷൃݴΑΓཁʣ
ࢀߟจݙ • ެࣜυΩϡϝϯτ • thoughtbot/factory_bot Getting Started • ϕϯνϚʔΫιʔείʔυ •
toshimaru/factory-bot-the-right-way • ͦͷଞ • Rails Ξϯνύλʔϯ - Ḋͼ͍ͭͨϑΝΫτϦʔ (factory_girl) - ΞδϟΠ ϧSEͷ༕ᓔ