FactoryBot the Right Way

5919537a0ecfa5d4dea704cf878ae90e?s=47 toshimaru
October 03, 2020

FactoryBot the Right Way

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

5919537a0ecfa5d4dea704cf878ae90e?s=128

toshimaru

October 03, 2020
Tweet

Transcript

  1. FactoryBot The Right Way 2020.10.3 Kaigi on Rails

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

    Engineer at MedPeer Inc. • ࠷ۙͷٕज़తؔ৺ • Vue.js, TypeScript, Ruby3ͷܕγεςϜ
  3. ࠾༻৘ใ https://medpeer.co.jp/recruit/ ։ൃϒϩά https://tech.medpeer.co.jp/ 8FSF)JSJOH

  4. FactoryBot The Right Way ࠓ೔͸ςετσʔλੜ੒ϥΠϒϥϦɺfactory_bot ͷਖ਼͍͠࢖͍ํ ʢRight Wayʣʹ͍͓ͭͯ࿩͠·͢ɻ

  5. Why? • ͜Ε·Ͱʮਖ਼͍͠RailsΞϓϦέʔγϣϯͷॻ͖ํʯ΍ʮਖ਼͍͠RSpec ςετͷॻ͖ํʯ͸਺ଟ͘঺հ͞Ε͖ͯͨɻ • Ұํɺʮਖ਼͍͠ςετσʔλͷ࡞Γํʯ͸͋·Γٞ࿦͞Εͯ͜ͳ͔ͬ ͨΑ͏ʹࢥ͑Δɻ

  6. Rails ඪ४తςετߏ੒ ϥΠϒϥϦ ςετର৅ 5FTUJOH'SBNFXPSL 34QFD NJOJUFTU 3BJMT"QQMJDBUJPO  .PEFM

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

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

    • Right Way: ެࣜυΩϡϝϯτ͔ΒͷൈਮɾҾ༻ • Good Way: Right Way ͔Βநग़ͨ͠άουϓϥΫςΟε 2. FactoryBot: The Wrong Way (Bad Way) • FactoryBotͷޡͬͨ࢖͍ํɾྑ͘ͳ͍࢖͍ํ
  9. ൃදͷߏ੒ ✅ The Right Way The Good Way The Wrong

    Way Կ͕μϝʁ/Ͳ͏͢Δʁ
  10. ϕϯνϚʔΫʹ͍ͭͯ • ϕϯνϚʔΫ؀ڥ • 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
  11. ✅ The Right Way factory_bot gemಋೖ Rails ͷ৔߹:

  12. ✅ The Right Way factory_bot methodಋೖ Ϋϥε໊Λলུͯ͠Factoryੜ੒ϝιουΛݺͼग़͢͜ͱ͕Ͱ͖ΔΑ͏ʹ:

  13. ✅ The Right Way Factoryఆٛ

  14. The Wrong Way ڊେσϑΥϧτσʔλ

  15. The Wrong Way ڊେσϑΥϧτσʔλ: Կ͕μϝʁ • ຊ౰ʹͦͷσʔλɺશ෦ඞཁͰ͔͢ʁ • ͦΕΛར༻͢ΔશͯͷςετέʔεͰ࠷খݶͷલఏঢ়ଶͷੜ੒ʹͳͬ ͍ͯΔ͔ʁ

    • ແବͳσʔλੜ੒ʹ͸ͳ͍ͬͯͳ͍͔ʁ • ແବͳσʔλੜ੒ • ؒҧͬͨલఏঢ়ଶΛ࡞Γग़͍ͯ͠Δ • ύϑΥʔϚϯεѱԽ
  16. The Wrong Way ڊେσϑΥϧτσʔλ: Ͳ͏͢Δʁ • ࠷খݶͷσʔληοτఆٛΛσϑΥϧτFactoryͱͯ͠ఆٛ͠·͠ΐ ͏ʢref. Defining factories

    > Best practicesʣ
  17. ✅ The Right Way Factory ϑΝΠϧ഑ஔ • Լهύεʹఆٛ͞ΕͨFactoryϑΝΠϧ͸ࣗಈతʹϩʔυ͞ΕΔ • test/factories.rb

    • spec/factories.rb • test/factories"/*.rb • spec/factories"/*.rb
  18. The Good Way Factory ϑΝΠϧ഑ஔ

  19. ✅ The Right Way Factory ੜ੒

  20. The Wrong Way ແବcreate • build ͰࡁΉͱ͜ΖΛͳΜͰ΋͔ΜͰ΋ create Λ࢖࣮ͬͯϨίʔυΛ ੜ੒ͯ͠͠·͏͜ͱ

  21. The Wrong Way ແବcreate: Կ͕μϝʁ • Ϣχοτςετͷඞཁ࠷௿ݶͳલఏ৚݅ʹͳ͍ͬͯͳ͍ • DBʹσʔλ͕ create

    ͞Ε͍ͯΔ͜ͱ͸ඞཁ৚݅Ͱ͸ͳ͍ • όϦσʔγϣϯͷςετͷ৔߹ɺvalid/invalidͳRubyΦϒδΣΫτͷঢ়ଶ ͑͞࡞͍ͬͯΕ͹Α͍ • SQL͕૸Δ෼ɺύϑΥʔϚϯεѱԽ • ςετ୯ମͰ͸໰୊ʹͳΔ͜ͱ͕ͳ͍͕ɺνϦπϞͰςετΛ஗ͤ͘͞Δ ཁҼʹͳΔ
  22. The Wrong Way ແବcreate: Կ͕μϝʁ • create, build ͦΕͧΕΛ࢖ͬͨ৔߹ͷSQLൺֱɿ

  23. The Wrong Way ແବcreate: Ͳ͏͢Δʁ • buildΛ࢖͍·͠ΐ͏

  24. The Wrong Way ແବcreate: ϕϯνϚʔΫ • In-Memory DB • File

    DB
  25. The Good Way build_stubbed ׆༻ • Լهͷ৔߹ͷςετ • ʮcreated_at ͸

    build ͡Όੜ੒͞Εͳ͍ͧ ʯ
  26. The Good Way build_stubbed ׆༻ • build_stubbed Λ׆༻͢Ε͹ɺελϒ͞Εͨid, timestampͱͱ΋ʹΦ ϒδΣΫτ͕ੜ੒͞ΕΔ

  27. ✅ The Right Way Trait (1)

  28. ✅ The Right Way Trait (2)

  29. The Wrong Way Too Many Traits • ྫ֎తͳ஋ɺڥք஋ͳͲΛtrait Ͱ΋Εͳ͘શ෦ఆٛ͠Α͏ͱ͢ Δ͜ͱ

  30. The Wrong Way Too Many Traits: Կ͕μϝʁ • traitఆٛ രൃ

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

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

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

    Model ͳΒ͵ Fat Factory ͷग़དྷ্͕Γʢref. Too Many Traitsʣ
  34. The Wrong Way DRY͡Όͳ͍traits: Ͳ͏͢Δʁ • ಈ࡞͢ΔσϑΥϧτͷঢ়ଶΛ Factory ͱͯ͠γϯϓϧʹఆٛ͠ɺͦΕ ʹಛघͳঢ়ଶΛఆٛ͢ΔtraitΛॏͶ͍ͯ͘

  35. The Wrong Way DRY͡Όͳ͍traits: Ͳ͏͢Δʁ •࡟আ͞Ε͍ͯΔAdminϢʔβʔ࡞੒ ߏஙύλʔϯɿ

  36. ✅ The Right Way sequence • sequence Λ࢖͏͜ͱͰσʔλʹγʔέϯγϟϧͳφϯόϦϯά͕Մ ೳʹ

  37. The Wrong Way ಠࣗsequence id • DBͰ࠾൪͞ΕΔidΛಠࣗͷidͰΦʔόʔϥΠυͯ͠͠·͏͜ͱ

  38. The Wrong Way ಠࣗsequence id: Կ͕μϝʁ • Ϩίʔυ࡞੒ͤͣͱ΋id࠾൪͕ߦΘΕΔ • ࣮ࡍͷDBͷڍಈͱҟͳΔͷͰɺϦΞϧͳςετʹͳΒͳ͍

  39. The Wrong Way ಠࣗsequence id: Ͳ͏͢Δʁ • ID࠾൪͸DBʹ೚ͤ·͠ΐ͏ • Stub͞Εͨ

    id ͕ཉ͍͠ͷͰ͋Ε͹ɺ build_stubbed Λར༻͠·͠ΐ ͏ʢref. build_stubbed ׆༻ʣ
  40. The Wrong Way ಉ͡஋ͷςετ

  41. The Wrong Way ಉ͡஋ͷςετ: Կ͕μϝʁ • name ͕ಉ͡஋Λੜ੒͍ͯ͠ΔͷͰɺuser1.name ͕ user2.name

    Ͱ ΋ user3.name Ͱ΋ςετ͕௨ͬͯ͠·͏
  42. The Wrong Way ಉ͡஋ͷςετ: Ͳ͏͢Δʁ • sequence Λ࢖ͬͯϢχʔΫੑͷ͋ΔσʔλΛੜ੒͢Δ • ϦΞϧϫʔϧυʹ͍ۙσʔλΛ

    faker Λ࢖ͬͯੜ੒͢Δ
  43. ✅ The Right Way build_list, create_list • build_list: ࢦఆճ਺ build

    ͨ͠ίϨΫγϣϯΛฦ͢ • create_list: ࢦఆճ਺ create ͨ͠ίϨΫγϣϯΛฦ͢
  44. The Wrong Way ແବlistੜ੒ • ෆඞཁʹ create_list ͯ͠ແବͳσʔλੜ੒Λߦ͏͜ͱ

  45. The Wrong Way ແବlistੜ੒: Կ͕μϝʁ • ͦͷσʔλɺશ෦ඞཁͰ͔͢ʁ (ref. ڊେσϑΥϧτσʔλ) •

    ແବͳσʔλੜ੒ʹ͸ͳ͍ͬͯͳ͍͔ʁ
  46. The Wrong Way ແବlistੜ੒: Ͳ͏͢Δʁ • transient + evaluator ͷػ

    ೳΛ࢖͍ؔ࿈ઌϨίʔυੜ ੒਺Λίϯτϩʔϧ͢Δ • ຊ౰ʹඞཁͳ਺͚ͩࢦఆ͠ ͯϨίʔυੜ੒͢ΔΑ͏ʹ ͢Δ
  47. ✅ The Right Way Association ఆٛ ↓ ্ड़ͷ post Factory͸

    Լهͱಉҙ ↓
  48. The Wrong Way Always Create Association • association ઌΛ create

    Ͱ࡞੒ͯ͠͠·͏͜ͱ
  49. The Wrong Way Always Create Association: Կ͕μϝʁ • build ͨ͠৔߹Ͱ΋ৗʹ

    createʢϨίʔυੜ੒ʣ͕൐͏
  50. The Wrong Way Always Create Association: Ͳ͏͢Δʁ • ૉ௚ʹ association

    ఆٛ͢Δ
  51. The Wrong Way Always Create Association: ϕϯνϚʔΫ • In-Memory DB

    • File DB
  52. ✅ The Right Way FactoryBot.lint • factory_bot ެ͕ࣜఏڙ͢ΔLintπʔϧ • FYI:

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

    Factory ΛҰ௨Γ create ͯ͠ɺinvalid ͳ Factory ఆٛΛ ݕ஌͢Δ • traits: true ΦϓγϣϯͰ trait ΋ؚΊͯݕࠪ͢Δ
  54. • rakeλεΫԽͯ͠ଞLintπʔϧ ʢrubocop౳ʣͱಉ༷ʹCIͷλε Ϋͷ̍ͭͱͯ͠৐͓ͤͯ͘ The Good Way FactoryBot.lint: rakeλεΫԽ

  55. ✅ FactoryBot The Right Way ·ͱΊ • ਖ਼͍͠લఏঢ়ଶΛਖ਼͘͠࡞Δ • ؆୯ʹσʔλఆٛɾϨίʔυੜ੒Ͱ͖ͯ͠·͏ނʹɺ؆୯ʹແବσʔλ

    Λ࡞Γग़͠΍͍͢ • ԼهΛҙࣝͯ͠ facotry_bot Λ࢖͍͜ͳͦ͏ • Factory ͷσϑΥϧτఆٛ͸ϛχϚϧͳσʔληοτʹͳ͍ͬͯΔ͔ʁ • ֤ʑͷςετέʔεͰ࢖͍ͬͯΔ Factory ͸࠷௿ݶͷલఏঢ়ଶΛ࡞Γग़ͤ ͍ͯΔ͔ʁ
  56. Happy Testing Life!

  57. Tips: FactoryGirl→FactoryBot • 2008೥ FactoryGirl஀ੜ • 2017೥ FactoryGirl͔ΒFactoryBotʹϦωʔϜ • ref.

    Project Naming History ⾠ OSS ͷ Naming ͸ؾΛ͚ͭΑ͏ʂ
  58. Tips: ςετσʔλͷϞοΫ/ελϒʹ͍ͭͯ “ϞοΫ/ελϒʹΑͬͯςετίʔυͷ࣮ߦεϐʔυ͕ૣ͘ͳΓςετ ͷ෼཭ੑ͸ߴ·ΔҰํɺϞοΫ/ελϒ͸ʻࣗ෼ͷໝ૝ɾئ๬͕࡞Γग़ ͨ͠ৼΔ෣͍ʼʹͳΔͷͰɺ࣮ࡍͷৼΔ෣͍ͱ͸ҟͳΔͱ͍͏ϦεΫ ΛሃΉɻ·ͨϞοΫ/ελϒΛ࡞Δ͜ͱͰɺϝϯςϯφϯεର৅ͱͳΔ ίʔυͷྔɾॏෳ͕ଟ͘ͳΔɻ ϩʔΧϧ؀ڥͰ࠶ݱͰ͖ΔRDB΍KVS͸ՄೳͳݶΓຊ෺Λ࢖͏ɻ” ʢfukabori.fm 13.

    ϖΞϓϩ΍ςετͷٙ໰ͱ͔ɺιϑτ΢ΣΞΤϯδχΞͷҭ੒ͱ͔ ͷtwada ͞ΜͷൃݴΑΓཁ໿ʣ
  59. ࢀߟจݙ • ެࣜυΩϡϝϯτ • thoughtbot/factory_bot Getting Started • ϕϯνϚʔΫιʔείʔυ •

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