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
【若手エンジニア応援LT会】ソフトウェアを学んできた私がインフラエンジニアを目指した理由
kazushi_ohata
0
150
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
AIチャットボット開発への生成AI活用
ryomrt
0
170
安心してください、日本語使えますよ―Ubuntu日本語Remix提供休止に寄せて― 2024-11-17
nobutomurata
1
990
rootlessコンテナのすゝめ - 研究室サーバーでもできる安全なコンテナ管理
kitsuya0828
3
380
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
元旅行会社の情シス部員が教えるおすすめなre:Inventへの行き方 / What is the most efficient way to re:Invent
naospon
2
340
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
TanStack Routerに移行するのかい しないのかい、どっちなんだい! / Are you going to migrate to TanStack Router or not? Which one is it?
kaminashi
0
580
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.8k
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
A designer walks into a library…
pauljervisheath
203
24k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Adopting Sorbet at Scale
ufuk
73
9.1k
Producing Creativity
orderedlist
PRO
341
39k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
A Philosophy of Restraint
colly
203
16k
Optimizing for Happiness
mojombo
376
70k
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ͷ༕ᓔ