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

RSpecでBDDをしよう

y@su
July 14, 2018

 RSpecでBDDをしよう

Rails Developers Meetup 2018 Day 3 Extreme

TDD→BDD→俺がやってるRSpecの書き方を狙ったけどRSpecの部分をやっていると時間が足り無さ過ぎて無理でしたw

y@su

July 14, 2018
Tweet

More Decks by y@su

Other Decks in Technology

Transcript

  1. ࠓ೔࿩͢͜ͱ TDD / BDD / RSpec ฉ͍ͯཉ͍͠ਓ෺૾ • एखͷRailsΤϯδχΞ(ब৬͢Δ·Ͱίʔυॻ͍ͯͳ͔ͬͨͱ͔) •

    RSpec͸୯७ʹςετίʔυΛॻ͘ҝʹ࢖͍ͬͯΔΑͱ͍͏ΤϯδχΞ • ͦΕҎ֎ͷਓͰ΋OKͰ͢ ※ ৭ʑ͋ͬͯRSpecʹ͸͋·Γݴٴ͍ͯ͠ͳ͍Ͱ͢ɾɾ
  2. TDD

  3. TDDͷྺ࢙(؆қ൛) w ೥%%.D$SBDLFO͕%JHJUBM$PNQVUFS1SPHSBNNJOHzΛग़൛ w ιϑτ΢ΣΞςετٕज़ʹ͍ͭͯॻ͔Εͨ࠷ॳͷॻ੶ w ೥୅͸σόοάɾςετɾɾɾ౳ͷ֓೦͕۠ผ͞Ε͍ͯͳ͔ͬͨ࣌୅ w ʮςετʯͱ͍͏༻ޠ͕࢖ΘΕ͍ͯͳ͍ ֘౰͢ΔՕॴͷষ໊͸l1SPHSBN$IFDLPVUz

     ʙ5%%͋·Γ࠾༻͞Εͳ͔ͬͨظؒʙ w ೥,FOU#FDL͕46OJUΛ։ൃ w ޙͷY6OJUͷݩʹͳΔϢχοτςετϑϨʔϜϫʔΫ w 5%%ͷݪܕͱͳΔϓϩάϥϛϯάख๏΍ςετΛར༻ͨ͠ϦϑΝΫλϦϯάͷߟ͑ํ͕ ஝ੵ͞Ε͍ͯ͘ɻ w %JHJUBM$PNQVUFS1SPHSBNNJOHzͷଘࡏ͕͋ΔͷͰɺ5%%Λൃ໌ͨ͠ͷͰ͸ͳ͘ɺ ʮ5%%Λ࠶ൃݟʯͨ͠ͱݴΘΕ͍ͯΔɻ
  4. TDDͷਐΊํ 1. ࠷ॳʹςετίʔυΛ1ͭॻ͘(ςετϑΝʔετ) 2. 1.Λ࣮ߦ͠ɺࣦഊ͢Δ͜ͱΛ֬ೝ͢Δ (Red) 3. 2.ͷςετ͕੒ޭ͢ΔΑ͏ʹ࠷খݶͷϓϩμΫτίʔυΛॻ͘ 4. 3.Λ࣮ߦ͠ɺ੒ޭ͢Δ͜ͱΛ֬ೝ͢Δ

    (Green) 5. ϓϩμΫτίʔυͷৼΔ෣͍Λม͑ͳ͍Α͏ʹϦϑΝΫλϦϯάΛߦ͏ (Refactor) 6. 1.ʹ໭Δ ͬ͘͟Γݴ͏ͱ͜ͷ։ൃαΠΫϧΛ܁Γฦ͍ͯ͘͠ͷ͕TDDͰ͢ɻ
  5. ࣮ߦ(RED) $ bin/rspec spec/models/user_spec.rb An error occurred while loading ./spec/models/user_spec.rb.

    Failure/Error: RSpec.describe User, type: :model do NameError: uninitialized constant User No examples found. Finished in 4.46 seconds (files took 8.57 seconds to load) 0 examples, 0 failures, 1 error occurred outside of examples ࣦഊ͢Δ͜ͱΛ֬ೝ
  6. ࣮ߦ(Green) $ bin/rspec spec/models/user_spec.rb 
 No examples found. Finished in

    2.69 seconds (files took 7.16 seconds to load) 0 examples, 0 failures ੒ޭ͢Δ͜ͱΛ֬ೝ
  7. #register_userͷςετΛ1ͭ௥Ճ require 'rails_helper' RSpec.describe User, type: :model do describe '#register_user'

    do let(:user) { create :user } it 'trueΛฦ͢͜ͱ' do expect(user.register_user('Ϣʔβʔ໊', '[email protected]')).to eq true end end end ॻ͘ςετ͸ͭͷΈ
  8. ࣮ߦ(RED) $ bin/rspec spec/models/user_spec.rb User #register_user trueΛฦ͢͜ͱ (FAILED - 1)

    Failures: 1) User#register_user trueΛฦ͢͜ͱ Failure/Error: expect(user.register_user('Ϣʔβʔ໊', '[email protected]')).to eq true NoMethodError: undefined method `register_user' for #<User:0x007fc64f14db98> Finished in 4.49 seconds (files took 10.77 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/models/user_spec.rb:7 # User#register_user trueΛฦ͢͜ͱ ࣦഊ͢Δ͜ͱΛ֬ೝ
  9. #register_userϝιουΛ௥Ճ class User < ApplicationRecord def register_user(name, email) true end

    end ςετΛ௨͢࠷খݶͷίʔυΛ࣮૷͢Δ͜ͱ ࠷খݶͷίʔυͳͷͰ͜ΕͰྑ͍
  10. ࣮ߦ(Green) $ bin/rspec spec/models/user_spec.rb User #register_user trueΛฦ͢͜ͱ Finished in 3.16

    seconds (files took 6.98 seconds to load) 1 example, 0 failures ੒ޭ͢Δ͜ͱΛ֬ೝ
  11. #register_userͷςετΛ1ͭ௥Ճ require 'rails_helper' RSpec.describe User, type: :model do describe '#register_user'

    do let(:user) { create :user } it 'trueΛฦ͢͜ͱ' do expect(user.register_user('Ϣʔβʔ໊', '[email protected]')).to eq true end it 'usersςʔϒϧʹσʔλΛ௥Ճ͍ͯ͠Δ͜ͱ' do expect { user.register_user('Ϣʔβʔ໊', '[email protected]') }.to change { described_class.where(name: 'Ϣʔβʔ໊', email: '[email protected]').exists? }.from(false).to(true) end end end ॻ͘ςετ͸΍͸ΓͭͷΈ
  12. ࣮ߦ(RED) $ bin/rspec spec/models/user_spec.rb User #register_user trueΛฦ͢͜ͱ usersςʔϒϧʹσʔλΛ௥Ճ͍ͯ͠Δ͜ͱ (FAILED -

    1) Failures: 1) User#register_user usersςʔϒϧʹσʔλΛ௥Ճ͍ͯ͠Δ͜ͱ Failure/Error: expect { user.register_user('Ϣʔβʔ໊', '[email protected]') }.to change { described_class.where(name: 'Ϣʔβʔ໊', email: '[email protected]').exists? }.from(false).to(true) expected `described_class.where(name: 'Ϣʔβʔ໊', email: '[email protected]').exists?` to have changed from false to true, but did not change Finished in 2.95 seconds (files took 9.34 seconds to load) 2 examples, 1 failure Failed examples: rspec ./spec/models/user_spec.rb:11 # User#register_user usersςʔϒϧʹσʔλΛ௥Ճ͍ͯ͠Δ͜ͱ ࣦഊ͢Δ͜ͱΛ֬ೝ
  13. #register_userϝιουΛߋ৽ class User < ApplicationRecord def register_user(name, email) user =

    User.new user.name = name user.email = email user.save end end ςετΛ௨͢࠷খݶͷίʔυΛ࣮૷͢Δ͜ͱ ͱʹ͔͘ಈ͘ίʔυΛॻ͚͹͍͍
  14. ࣮ߦ(Green) $ bin/rspec spec/models/user_spec.rb User #register_user trueΛฦ͢͜ͱ usersςʔϒϧʹσʔλΛ௥Ճ͍ͯ͠Δ͜ͱ Finished in

    3.5 seconds (files took 8.05 seconds to load) 2 examples, 0 failures ੒ޭ͢Δ͜ͱΛ֬ೝ
  15. ϦϑΝΫλϦϯά(Refactor) class User < ApplicationRecord def register_user(name, email) user =

    User.new(name: name, email: email) user.save end end ;ͭ͏ʹOFXͰύϥϝʔλ౉ͤ͹͍͍ΑͶ
  16. ࣮ߦ(Green) $ bin/rspec spec/models/user_spec.rb User #register_user trueΛฦ͢͜ͱ usersςʔϒϧʹσʔλΛ௥Ճ͍ͯ͠Δ͜ͱ Finished in

    3.5 seconds (files took 8.31 seconds to load) 2 examples, 0 failures ੒ޭ͢Δ͜ͱΛ֬ೝ 4QFD͕͋Ε͹ϦϑΝΫλϦϯά͠΍͍͢
  17. “TDD͸ɺςετͱ͍͏ݴ༿͕࣋ͭχϡΞϯεͷ໰୊ʹ௚໘͍ͯ͠·ͨ͠ɻςε τͱ͍͏୯ޠͱɺͦΕʹ·ͭΘΔޠኮΛ࢖͍ͬͯΔݶΓɺTDD͸ʮٕज़ऀ͕ɺ Ͳ͏࡞Δ͔ʯʹ࢖͏΋ͷͰ͋Δͱ͍͏ઌೖ؍ʹٯΒ͑ͣɺTDDͷਖ਼֬ͳཧղͱ ܒ໤ʹ͸ࢸΒͳ͍ͷͰ͸ͳ͍͔ͱ͍͏໰୊ҙࣝͰ͢ɻ” ൈਮ:: Kent Beck “Test-Driven Development: By

    Example” ෇࿥C TDDࢧ࣋೿ͷίϛϡχςΟͰ͜ͷ݅͸ٞ࿦ʹͳͬͨɻ ʮTDDʯͰ͸ବ໨ͰޠኮΛม͑ͳ͚Ε͹ͳΒͳ͍ɻ TDDͷTͷ෦෼ʹTestΛ࢖͍ͬͯΔݶΓɺTDD͕ਖ਼͘͠ཧղ ͞ΕΔ͜ͱ͸೉͍͠ɻ ͜͜ͰDanNorth͕ൃݟͨ͠ޠኮ͕ɺɺɺ TDDͱ͍͏ݴ༿๊͕͑Δ໰୊
  18. BDD

  19. ޠኮͷมߋͱRSpecͷ஀ੜ BTTFSUJPOˠFYQFDUBUJPO UFTUˠFYBNQMF UFTUDBTFˠFYBNQMFHSPVQ UFTUDMBTTˠTQFD w +#FIBWF +BWB ͕։ൃ͞Εͨ %BO/PSUI

    5%%ˠ#%%͚ͩͰͳ͘ɺ ؒҧͬͨઌೖ؍Λ༩͑ͳ͍Α͏ ʹɺޠኮΛมߋ 5%%ͷࠒͱ͸ޠኮΛஔ͖׵͑ͨςετϑϨʔϜϫʔΫΛ࡞੒ w 34QFD 3VCZ ͷ஀ੜ΁ %BWF"TUFMTɺ"TMBL)FMMFT”Z