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
社内イベント管理システムを1週間でAKSからACAに移行した話し
shingo_kawahara
0
180
UI State設計とテスト方針
rmakiyama
2
580
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
260
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
MLOps の現場から
asei
6
640
10分で学ぶKubernetesコンテナセキュリティ/10min-k8s-container-sec
mochizuki875
3
330
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
祝!Iceberg祭開幕!re:Invent 2024データレイク関連アップデート10分総ざらい
kniino
3
260
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
220
Jetpack Composeで始めるServer Cache State
ogaclejapan
2
170
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
220
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
RailsConf 2023
tenderlove
29
940
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Making Projects Easy
brettharned
116
5.9k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
0
98
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Docker and Python
trallard
42
3.1k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
The Cost Of JavaScript in 2023
addyosmani
45
7k
GitHub's CSS Performance
jonrohan
1030
460k
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ͷ༕ᓔ