Slide 1

Slide 1 text

Make tests run faster Omotesando.rb #88 2023/08/03 1

Slide 2

Slide 2 text

ࣗݾ঺հ • ໊લ: ਖ਼ಙ ޼(aka: ਆ଎) • ձࣾ: - • GitHub: @sinsoku (ը૾ӈ্) • Twitter: @sinsoku_listy (ը૾ӈԼ) • ޷͖ͳݴޠ: Rust • Railsྺ: 8೥͘Β͍ 2

Slide 3

Slide 3 text

͓લʹ଍Γͳ͍΋ͷ͸ɺͦΕ͸―― ৘೤ɺࢥ૝ɺཧ೦ɺ಄೴ɺؾ඼ɺ༏խ͞ɺۈษ ͞! ͦͯ͠ԿΑΓ΋ ―― ଎͕͞଍Γͳ͍!! -- ετϨΠτɾΫʔΨʔʢεΫϥΠυʣ 3

Slide 4

Slide 4 text

ςετͷ࣮ߦ࣌ؒΛ଎͍ͨ͘͠ 4

Slide 5

Slide 5 text

௚ۙͰ΍ͬͨ͜ͱͷ঺հ1 1. Database Cleaner -> DatabaseRewinder 2. γʔυʹґଘ͢Δςετͷमਖ਼ 1 ࠓճ଎ͨ͘͠ͷ͸ʮશςετͷ࣮ߦ࣌ؒʯͰ͸ͳ͘ʮϑΝΠϧΛࢦఆͨ͠ςετͷ࣮ߦ࣌ؒʯʹͳΔɻ 5

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

มߋ͸Ϋϥε໊ͷมߋ͚ͩ SeedFu.seed(SeedFu.fixture_paths, /#{filenames.join('|')}/) - DatabaseCleaner.clean_with :truncation, { - except: %w[ - foo - bar (ུʣ + DatabaseRewinder.clean_with :truncation, except: %w[ + foo + bar 7

Slide 8

Slide 8 text

DatabaseRewinder ΁ͷҠߦ݁Ռ SQLΛ൐Θͳ͍ςετͰൺֱ͢Δͱ໿1.5ඵΛߴ଎ԽͰ͖ͨɻ $ rspec spec/lib/no_sql_spec.rb 3.38 -> 1.88 seconds 8

Slide 9

Slide 9 text

ʢຊ୊ʣγʔυʹґଘ͢Δςετ 9

Slide 10

Slide 10 text

RSpecͰγʔυΛಡΈࠐΊΔ ςετ؀ڥͰ΋ຊ൪؀ڥͱಉ͡γʔυσʔλΛ࢖͑ͯศརʂ # spec/rails_helper.rb RSpec.configure do |config| config.before(:suite) do SeedFu.quiet = true SeedFu.seed end end 10

Slide 11

Slide 11 text

γʔυґଘͷ໰୊ • ৗʹશͯͷγʔυΛಡΈࠐΜͰ͠·͏ • σʔλྔ͕ଟ͍ͱ஗͍ ! • ςετίʔυ͕ಡΈਏ͍ • γʔυલఏͰ Prefecture.first ͱॻ͘ͱத਎͕෼͔Βͳ͍ • ςετͱγʔυͷ྆ํΛಡΉඞཁ͕͋Δ 11

Slide 12

Slide 12 text

γʔυґଘͷमਖ਼ γʔυΛಡΈࠐ·ͣɺ֤ςετͰඞཁͳϨίʔυ͚ͩΛ࡞੒͢ Δɻ ͜Εʹؔ࿈ͯ͠ɺҎԼͷରԠ΋ඞཁʹͳΔɻ 1. FactoryBot Ͱ find_or_create Λ࣮ݱ͢Δ 2. ओΩʔʢidʣʹґଘ͢ΔॲཧΛ࡟আ͢Δ 12

Slide 13

Slide 13 text

FactoryBotͰfind_or_create Λ࣮ݱ2 # factory :foo do # extend FactoryBotSingleFactory # end module FactoryBotSingleFactory def self.extended(obj) obj.to_create do |instance| attributes = instance.class.find_or_create_by(instance.attributes.compact).attributes instance.attributes = attributes.except('id') instance.id = attributes['id'] # id can't be mass-assigned instance.instance_variable_set('@new_record', false) # marks record as persisted end end end 2 https://gist.github.com/ReganRyanNZ/71e4128d34459d5b00dcb27d2ba1757e Λࢀߟʹͨ͠ɻ 13

Slide 14

Slide 14 text

ओΩʔͷґଘΛ࡟আ 1 id Λݻఆʹ͢Δͷ͸೉͍ͨ͠ΊɺओΩʔʹґଘ͠ͳ͍ܗʹ௚͢ɻ class Prefecture def tokyo? - id == 13 + name == '౦ژ' end end 14

Slide 15

Slide 15 text

ओΩʔͷґଘΛ࡟আ 2 ΧϥϜΛ௥Ճͯ͠ɺϝιουΛফ͢ํ๏΋͋Δɻ class AddTokyoToPrefecture < ActiveRecord::Migration[7.0] def change add_column :prefectures, :tokyo, :boolean up_only do execute 'UPDATE prefectures SET tokyo = 1 WHERE id == 13;' end change_column_null :prefectures, :tokyo, false end end 15

Slide 16

Slide 16 text

γʔυґଘͷςετΛ௚͢ ͍͖ͳΓ find_by ͯ͠Δίʔυྫɻ - prefecture = Prefecture.find_by(name: '౦ژ౎') + prefecture = create(:prefecture, name: '౦ژ౎') ݻఆ஋Λ౉ͯ͠Δίʔυྫɻ let(:query) do { - prefecture_ids: [1], + prefecture_ids: [create(:prefecture, :hokkaido).id], 16

Slide 17

Slide 17 text

൒෼͘Β͍γʔυґଘΛमਖ਼ͨ݁͠Ռ3 DatabaseRewinder Ͱ࢖ͬͨςετͰվΊͯௐ΂·ͨ͠ɻ $ rspec spec/lib/no_sql_spec.rb 1.88 -> 1.04 seconds 3 ͪͳΈʹ SeedFu.seed Λফͨ͠ͱ͖͸ 0.23ඵ Ͱ͢ɻ 17

Slide 18

Slide 18 text

·ͱΊ • ! DatabaseRewinder ͸଎͍ • " ςετͰγʔυΛ࢖͏ͷ͸ආ͚Δ • # ओΩʔʢidʣͷґଘ͸ආ͚Δ ͦͯ͠ɺςετ͔ΒγʔυͷґଘΛফ͢ઓ͍͸ଓ͘... 18