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

Make tests run faster

Make tests run faster

【オフライン開催】Omotesando.rb #88
https://omotesandorb.connpass.com/event/290594/

Takumi Shotoku

August 04, 2023
Tweet

More Decks by Takumi Shotoku

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ໊લ: ਖ਼ಙ ޼(aka: ਆ଎) • ձࣾ: - •

    GitHub: @sinsoku (ը૾ӈ্) • Twitter: @sinsoku_listy (ը૾ӈԼ) • ޷͖ͳݴޠ: Rust • Railsྺ: 8೥͘Β͍ 2
  2. 6

  3. γʔυґଘͷ໰୊ • ৗʹશͯͷγʔυΛಡΈࠐΜͰ͠·͏ • σʔλྔ͕ଟ͍ͱ஗͍ ! • ςετίʔυ͕ಡΈਏ͍ • γʔυલఏͰ

    Prefecture.first ͱॻ͘ͱத਎͕෼͔Βͳ͍ • ςετͱγʔυͷ྆ํΛಡΉඞཁ͕͋Δ 11
  4. 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
  5. ओΩʔͷґଘΛ࡟আ 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
  6. γʔυґଘͷςετΛ௚͢ ͍͖ͳΓ find_by ͯ͠Δίʔυྫɻ - prefecture = Prefecture.find_by(name: '౦ژ౎') +

    prefecture = create(:prefecture, name: '౦ژ౎') ݻఆ஋Λ౉ͯ͠Δίʔυྫɻ let(:query) do { - prefecture_ids: [1], + prefecture_ids: [create(:prefecture, :hokkaido).id], 16
  7. ·ͱΊ • ! DatabaseRewinder ͸଎͍ • " ςετͰγʔυΛ࢖͏ͷ͸ආ͚Δ • #

    ओΩʔʢidʣͷґଘ͸ආ͚Δ ͦͯ͠ɺςετ͔ΒγʔυͷґଘΛফ͢ઓ͍͸ଓ͘... 18