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
3.2k
RubyKaigiのプロポーザルを通したい。 / rubykaigi-proposal
toshimaru
3
3.3k
10年モノのレガシーPHPアプリケーションを移植しきるまでの泥臭くも長い軌跡 / legacy-php-app-migration
toshimaru
1
2.4k
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
20k
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
PaaSの歴史と、 アプリケーションプラットフォームのこれから
jacopen
7
1.4k
OPENLOGI Company Profile for engineer
hr01
1
18k
商品レコメンドでのexplicit negative feedbackの活用
alpicola
1
340
信頼されるためにやったこと、 やらなかったこと。/What we did to be trusted, What we did not do.
bitkey
PRO
0
2.1k
Oracle Base Database Service:サービス概要のご紹介
oracle4engineer
PRO
1
16k
30分でわかる「リスクから学ぶKubernetesコンテナセキュリティ」/30min-k8s-container-sec
mochizuki875
3
440
デジタルアイデンティティ技術 認可・ID連携・認証 応用 / 20250114-OIDF-J-EduWG-TechSWG
oidfj
2
610
完全自律型AIエージェントとAgentic Workflow〜ワークフロー構築という現実解
pharma_x_tech
0
330
今から、 今だからこそ始める Terraform で Azure 管理 / Managing Azure with Terraform: The Perfect Time to Start
nnstt1
0
200
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
6
54k
20250116_JAWS_Osaka
takuyay0ne
2
200
KMP with Crashlytics
sansantech
PRO
0
240
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
96
5.3k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
3
360
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Typedesign – Prime Four
hannesfritz
40
2.5k
Scaling GitHub
holman
459
140k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
860
Writing Fast Ruby
sferik
628
61k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
230
52k
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ͷ༕ᓔ