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.8k
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
1.8k
RubyKaigiのプロポーザルを通したい。 / rubykaigi-proposal
toshimaru
3
3.1k
10年モノのレガシーPHPアプリケーションを移植しきるまでの泥臭くも長い軌跡 / legacy-php-app-migration
toshimaru
1
2.1k
Faster Pull Request Reviews 〜ハイパフォーマンスチームへの道〜 / Faster Pull Request Reviews
toshimaru
7
2.5k
medpeer.jp 開発を加速させるエンジニアリング施策 / medpeer engineering kaigi on rails 2021
toshimaru
2
5.9k
アラサーエンジニアの生存戦略 / 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
CysharpのOSS群から見るModern C#の現在地
neuecc
2
3.4k
Introduction to Works of ML Engineer in LY Corporation
lycorp_recruit_jp
0
130
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
530
いざ、BSC討伐の旅
nikinusu
2
780
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
130
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
TypeScript、上達の瞬間
sadnessojisan
46
13k
SSMRunbook作成の勘所_20241120
koichiotomo
3
160
飲食店データの分析事例とそれを支えるデータ基盤
kimujun
0
120
SRE×AIOpsを始めよう!GuardDutyによるお手軽脅威検出
amixedcolor
0
140
複雑なState管理からの脱却
sansantech
PRO
1
150
DynamoDB でスロットリングが発生したとき_大盛りver/when_throttling_occurs_in_dynamodb_long
emiki
1
420
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
10 Git Anti Patterns You Should be Aware of
lemiorhan
655
59k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Documentation Writing (for coders)
carmenintech
65
4.4k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Agile that works and the tools we love
rasmusluckow
327
21k
Become a Pro
speakerdeck
PRO
25
5k
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ͷ༕ᓔ