Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FactoryBot the Right Way

Avatar for toshimaru toshimaru
October 03, 2020

FactoryBot the Right Way

Kaigi on Rails ( https://kaigionrails.org )の登壇資料です。

Avatar for toshimaru

toshimaru

October 03, 2020
Tweet

More Decks by toshimaru

Other Decks in Technology

Transcript

  1. Self Introduction • GitHub: @toshimaru • Twitter: @toshimaru_e • Software

    Engineer at MedPeer Inc. • ࠷ۙͷٕज़తؔ৺ • Vue.js, TypeScript, Ruby3ͷܕγεςϜ
  2. Rails ඪ४తςετߏ੒ ϥΠϒϥϦ ςετର৅ 5FTUJOH'SBNFXPSL 34QFD NJOJUFTU 3BJMT"QQMJDBUJPO  .PEFM

     $POUSPMMFS  7JFX 'JYUVSF 'BDUPSZ#PU 3BJMT'JYUVSFT  .PEFM0CKFDU  %BUBCBTF
  3. Why Fixture Matters • ςετͷલఏͱͳΔঢ়ଶΛ࡞Γग़͢ͷ͕ Fixture • લఏ৚݅Λؒҧ͑͹ɺͦͷޙͷςετ΋ഁ୼͢Δ • Fixture

    Ͱਖ਼͍͠લఏঢ়ଶΛ࡞Γग़͢͜ͱ͕ॏཁ • ςετຖʹԿ౓΋܁Γฦ͠ݺ͹ΕΔ • ඇޮ཰ͳσʔλੜ੒Λ͍ͯͨ͠৔߹ɺςετΛ஗ͤ͘͞ΔཁҼʹ
  4. ࿩͢͜ͱ 1. FactoryBot: The Right Way / The Good Way

    • Right Way: ެࣜυΩϡϝϯτ͔ΒͷൈਮɾҾ༻ • Good Way: Right Way ͔Βநग़ͨ͠άουϓϥΫςΟε 2. FactoryBot: The Wrong Way (Bad Way) • FactoryBotͷޡͬͨ࢖͍ํɾྑ͘ͳ͍࢖͍ํ
  5. ϕϯνϚʔΫʹ͍ͭͯ • ϕϯνϚʔΫ؀ڥ • 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
  6. The Wrong Way ڊେσϑΥϧτσʔλ: Կ͕μϝʁ • ຊ౰ʹͦͷσʔλɺશ෦ඞཁͰ͔͢ʁ • ͦΕΛར༻͢ΔશͯͷςετέʔεͰ࠷খݶͷલఏঢ়ଶͷੜ੒ʹͳͬ ͍ͯΔ͔ʁ

    • ແବͳσʔλੜ੒ʹ͸ͳ͍ͬͯͳ͍͔ʁ • ແବͳσʔλੜ੒ • ؒҧͬͨલఏঢ়ଶΛ࡞Γग़͍ͯ͠Δ • ύϑΥʔϚϯεѱԽ
  7. The Wrong Way ແବcreate: Կ͕μϝʁ • Ϣχοτςετͷඞཁ࠷௿ݶͳલఏ৚݅ʹͳ͍ͬͯͳ͍ • DBʹσʔλ͕ create

    ͞Ε͍ͯΔ͜ͱ͸ඞཁ৚݅Ͱ͸ͳ͍ • όϦσʔγϣϯͷςετͷ৔߹ɺvalid/invalidͳRubyΦϒδΣΫτͷঢ়ଶ ͑͞࡞͍ͬͯΕ͹Α͍ • SQL͕૸Δ෼ɺύϑΥʔϚϯεѱԽ • ςετ୯ମͰ͸໰୊ʹͳΔ͜ͱ͕ͳ͍͕ɺνϦπϞͰςετΛ஗ͤ͘͞Δ ཁҼʹͳΔ
  8. The Wrong Way Too Many Traits: Կ͕μϝʁ • traitఆٛ രൃ

    • Fat Model ͳΒ͵ Fat Factory ͷग़དྷ্͕Γ • Fat Factory • ຊ౰ʹඞཁͳ trait ͕ݟ͚ͭʹ͘͘ͳΔ • ͳΜͰ΋͔ΜͰ΋શͯͷύλʔϯΛ trait ʹدͤͯॻ͖ͨ͘ͳͬͪΌ ͏පؾ͕ൃ঱͢Δ
  9. The Wrong Way Too Many Traits: Ͳ͏͢Δʁ • ࢖͍ճͤΔ൚༻తͳৼΔ෣͍ͷΈΛ trait

    ʹఆٛ͢Δ • ྫ֎తͳ஋͸ݸผͷςετʹدͤΔ • ྫ: ϞσϧͷόϦσʔγϣϯςετ
  10. The Wrong Way DRY͡Όͳ͍traits • DRY (Don't repeat yourself) Ͱ͸ͳ͍

    traitɺॏෳͨ͠ trait ͕ଟ͘ఆٛ͞Ε͍ͯ Δঢ়ଶͷ͜ͱ
  11. The Wrong Way DRY͡Όͳ͍traits: Կ͕μϝʁ • traitఆٛ ૊Έ߹Θͤരൃ • Fat

    Model ͳΒ͵ Fat Factory ͷग़དྷ্͕Γʢref. Too Many Traitsʣ
  12. The Wrong Way ಠࣗsequence id: Ͳ͏͢Δʁ • ID࠾൪͸DBʹ೚ͤ·͠ΐ͏ • Stub͞Εͨ

    id ͕ཉ͍͠ͷͰ͋Ε͹ɺ build_stubbed Λར༻͠·͠ΐ ͏ʢref. build_stubbed ׆༻ʣ
  13. ✅ The Right Way build_list, create_list • build_list: ࢦఆճ਺ build

    ͨ͠ίϨΫγϣϯΛฦ͢ • create_list: ࢦఆճ਺ create ͨ͠ίϨΫγϣϯΛฦ͢
  14. The Wrong Way ແବlistੜ੒: Ͳ͏͢Δʁ • transient + evaluator ͷػ

    ೳΛ࢖͍ؔ࿈ઌϨίʔυੜ ੒਺Λίϯτϩʔϧ͢Δ • ຊ౰ʹඞཁͳ਺͚ͩࢦఆ͠ ͯϨίʔυੜ੒͢ΔΑ͏ʹ ͢Δ
  15. ✅ The Right Way FactoryBot.lint • factory_bot ެ͕ࣜఏڙ͢ΔLintπʔϧ • FYI:

    rubocop-rspec ʹ FactoryBot ༻ͷϧʔϧ΋ಉࠝ͞Ε͍ͯΔ • ref. RuboCop"::Cop"::RSpec"::FactoryBot
  16. ✅ The Right Way FactoryBot.lint: how it works • ఆٛ͞Εͨ

    Factory ΛҰ௨Γ create ͯ͠ɺinvalid ͳ Factory ఆٛΛ ݕ஌͢Δ • traits: true ΦϓγϣϯͰ trait ΋ؚΊͯݕࠪ͢Δ
  17. ✅ FactoryBot The Right Way ·ͱΊ • ਖ਼͍͠લఏঢ়ଶΛਖ਼͘͠࡞Δ • ؆୯ʹσʔλఆٛɾϨίʔυੜ੒Ͱ͖ͯ͠·͏ނʹɺ؆୯ʹແବσʔλ

    Λ࡞Γग़͠΍͍͢ • ԼهΛҙࣝͯ͠ facotry_bot Λ࢖͍͜ͳͦ͏ • Factory ͷσϑΥϧτఆٛ͸ϛχϚϧͳσʔληοτʹͳ͍ͬͯΔ͔ʁ • ֤ʑͷςετέʔεͰ࢖͍ͬͯΔ Factory ͸࠷௿ݶͷલఏঢ়ଶΛ࡞Γग़ͤ ͍ͯΔ͔ʁ
  18. ࢀߟจݙ • ެࣜυΩϡϝϯτ • thoughtbot/factory_bot Getting Started • ϕϯνϚʔΫιʔείʔυ •

    toshimaru/factory-bot-the-right-way • ͦͷଞ • Rails Ξϯνύλʔϯ - Ḋͼ͍ͭͨϑΝΫτϦʔ (factory_girl) - ΞδϟΠ ϧSEͷ༕ᓔ