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

RSpecとMinitest、使うならどっち? / #kanrk06

RSpecとMinitest、使うならどっち? / #kanrk06

関西Ruby会議06で使用したスライドです。
http://regional.rubykaigi.org/kansai06/

動画版はこちらです。
https://www.youtube.com/watch?v=XAzzA4la59E

スライド作成の裏話等はブログに書いています。
http://blog.jnito.com/entry/2015/07/13/073458

Junichi Ito

July 11, 2015
Tweet

More Decks by Junichi Ito

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ҏ౻३Ұ • Blog: give IT a try •

    Twitter: @jnchito • RailsϓϩάϥϚ • େࡕ෎๛தࢢग़਎ • ฌݿݝ੢࿬ࢢࡏॅ
  2. ͜Ε͔Β͓࿩͢͠Δ಺༰ • ςεςΟϯάϑϨʔϜϫʔΫͷ໾ׂ • RSpec ͱ Minitest ͷ؆୯ͳίʔυྫ • γϯλοΫεͷൺֱʢxUnitܗࣜ

    vs Specܗࣜʣ • ϑϨʔϜϫʔΫͷൺֱʢRSpec vs Mintiestʣ • ߟ࡯ɿRSpec ͸ͳͥҒ͍ਓʹ޷͔Εͳ͍ͷ͔ʁ • ·ͱΊɿ࢖͏ͳΒͲͬͪʁ
  3. ςετର৅ͷϓϩάϥϜ class Greeter def say 'Hello, world!' end end ɾGreeter

    Ϋϥε ɾsay ϝιου ɾ‘Hello, world!’ ɹΛฦ͢
  4. ্͕ MinitestɺԼ͕ RSpec class GreeterTest < Minitest::Test def test_says_hello_world greeter

    = Greeter.new assert_equal 'Hello, world!', greeter.say end end describe Greeter do it 'says "Hello, world!"' do greeter = Greeter.new expect(greeter.say).to eq 'Hello, world!' end end ↑ xUnitܗࣜ ↑ Specܗࣜ
  5. Minitest ͸ Specܗࣜ Ͱ΋ॻ͚Δ describe Greeter do it 'says "Hello,

    world!"' do greeter = Greeter.new greeter.say.must_equal 'Hello, world!' end end describe Greeter do it 'says "Hello, world!"' do greeter = Greeter.new expect(greeter.say).to eq 'Hello, world!' end end ↑ RSpec ʹͦͬ͘Γʂʂ
  6. xUnitܗࣜ vs Specܗࣜ Y6OJUܗࣜ 4QFDܗࣜ γϯλοΫε ϐϡΞ3VCZ %4-ʢυΩϡϝϯτ෩ʣ शಘͷϋʔυϧ 3VCZͷจ๏͕Θ͔Ε͹0,

    %4-Λ֮͑Δඞཁ͋Γ ϩδοΫͷࣗ༝౓ 3VCZͷݴޠػೳΛ ϑϧ׆༻Ͱ͖Δ %4-ʹ੍໿͞Ε΍͍͢ ݕূϝιου BTTFSU@FRVBM" # FYQFDU # UPFR" ·ͨ͸#NVTU@FRVBM" ςετͷߏ଄Խ Ϋϥεͷܧঝ·ͨ͸ ແཧʹߏ଄Խ͠ͳ͍ ϒϩοΫΛωετͤ͞Δ ໋໊ͷࣗ༝౓ Ϋϥε໊΍ϝιου໊ͷ ॏෳʹ஫ҙ͕ඞཁ ໋໊͸ൺֱతࣗ༝ อकੑ ڽͬͨίʔυΛॻ͔ΕΔͱ ղੳ͕େมͦ͏ %4-ͷϧʔϧΛҳ୤ ͠ͳ͚Ε͹อक͠΍ͦ͢͏
  7. ςετΛߏ଄Խ͢Δྫ class User def initialize(name:, age:) @name = name @age

    = age end def greet if @age <= 12 "΅͘͸#{@name}ͩΑɻ" else "๻͸#{@name}Ͱ͢ɻ" end end end ৚݅ʹԠͯ͡ςετίʔυ Λߏ଄Խ͍ͨ͠
  8. Specܗࣜ Ͱߏ଄Խ͢Δ৔߹ describe User do before do puts 'Test start.'

    end describe '#greet' do before do puts 'Test start in #greet.' end let(:user) { User.new(name: 'ͨΖ͏', age: age) } subject { user.greet } context '12ࡀҎԼͷ৔߹' do let(:age) { 12 } it { is_expected.to eq '΅͘͸ͨΖ͏ͩΑɻ' } end context '13ࡀҎ্ͷ৔߹' do let(:age) { 13 } it { is_expected.to eq '๻͸ͨΖ͏Ͱ͢ɻ' } end end end ϒϩοΫͰάϧʔϓԽ (ߏ଄Խ)
  9. xUnitܗࣜ Ͱߏ଄Խ͢Δ৔߹ class UserTest < Minitest::Test def setup puts 'Test

    start.' end end module CommonMethods def setup super puts 'Test start in #greet.' @user = User.new(name: 'ͨΖ͏', age: @age) end end class UserTestWhenUnder12 < UserTest prepend CommonMethods def setup super @age = 12 end def test_greet assert_equal '΅͘͸ͨΖ͏ͩΑɻ', @user.greet end end class UserTestWhenAbove13 < UserTest # ͭͮ͘… ܧঝͨ͠Γɺ Ϟδϡʔϧ࡞ͬͨΓɺ prepend ͨ͠Γ super ݺΜͩΓ…
  10. Ͱ΋ߏ଄Խ͠ͳ͍ํ͕γϯϓϧ class UserTestSimplified < Minitest::Test def setup puts 'Test start.'

    end def test_greet puts 'Test start in #greet.' user = User.new(name: 'ͨΖ͏', age: 12) assert_equal '΅͘͸ͨΖ͏ͩΑɻ', user.greet user = User.new(name: 'ͨΖ͏', age: 13) assert_equal '๻͸ͨΖ͏Ͱ͢ɻ', user.greet end end ͥΜͿϑϥοτͰ͑͑΍Μ
  11. xUnitܗࣜ vs Specܗࣜ Y6OJUܗࣜ 4QFDܗࣜ γϯλοΫε ϐϡΞ3VCZ %4-ʢυΩϡϝϯτ෩ʣ शಘͷϋʔυϧ 3VCZͷจ๏͕Θ͔Ε͹0,

    %4-Λ֮͑Δඞཁ͋Γ ϩδοΫͷࣗ༝౓ 3VCZͷݴޠػೳΛ ϑϧ׆༻Ͱ͖Δ %4-ʹ੍໿͞Ε΍͍͢ ݕূϝιου BTTFSU@FRVBM" # FYQFDU # UPFR" ·ͨ͸#NVTU@FRVBM" ςετͷߏ଄Խ Ϋϥεͷܧঝ·ͨ͸ ແཧʹߏ଄Խ͠ͳ͍ ϒϩοΫΛωετͤ͞Δ ໋໊ͷࣗ༝౓ Ϋϥε໊΍ϝιου໊ͷ ॏෳʹ஫ҙ͕ඞཁ ໋໊͸ൺֱతࣗ༝
  12. xUnitܗࣜ vs Specܗࣜ Y6OJUܗࣜ 4QFDܗࣜ γϯλοΫε ϐϡΞ3VCZ %4-ʢυΩϡϝϯτ෩ʣ शಘͷϋʔυϧ 3VCZͷจ๏͕Θ͔Ε͹0,

    %4-Λ֮͑Δඞཁ͋Γ ϩδοΫͷࣗ༝౓ 3VCZͷݴޠػೳΛ ϑϧ׆༻Ͱ͖Δ %4-ʹ੍໿͞Ε΍͍͢ ݕূϝιου BTTFSU@FRVBM" # FYQFDU # UPFR" ·ͨ͸#NVTU@FRVBM" ςετͷߏ଄Խ Ϋϥεͷܧঝ·ͨ͸ ແཧʹߏ଄Խ͠ͳ͍ ϒϩοΫΛωετͤ͞Δ ໋໊ͷࣗ༝౓ Ϋϥε໊΍ϝιου໊ͷ ॏෳʹ஫ҙ͕ඞཁ ໋໊͸ൺֱతࣗ༝ อकੑ ڽͬͨίʔυΛॻ͔ΕΔͱ ղੳ͕େมͦ͏ %4-ͷϧʔϧΛҳ୤ ͠ͳ͚Ε͹อक͠΍ͦ͢͏
  13. RSpec vs Minitest 34QFD .JOJUFTU ར༻཰ɺ৘ใྔ ߴ͍ɺଟ͍ ௿͍ɺগͳ͍ σϑΥϧτͷػೳ ๛෋

    ඞཁ࠷খݶ ʢϓϥάΠϯͰ௥Ճʣ ޙํޓ׵ੑͷҡ࣋ Կ౓͔େ͖ͳมߋ͋Γ ଟগ͋Γ ఏڙܗଶ 3VCZඪ४Ͱ͸ͳ͍ ཁHFNΠϯετʔϧ 3VCZඪ४όϯυϧ ·ͨ͸HFNΠϯετʔϧ 3BJMTͱͷؔ܎ ඪ४Ͱ͸ͳ͍ ཁηοτΞοϓ 3BJMTඪ४ 3BJMTνϡʔτϦΞϧͰ΋࠾༻ ຊମͷίʔυ ଟ͍ɺෳࡶ গͳ͍ɺγϯϓϧ ઃܭࢥ૝ ͢΂͕ͯୈҰڃΫϥε ΦϒδΣΫτ +VTU3VCZ
  14. RSpec vs Minitest 34QFD .JOJUFTU ར༻཰ɺ৘ใྔ ߴ͍ɺଟ͍ ௿͍ɺগͳ͍ σϑΥϧτͷػೳ ๛෋

    ඞཁ࠷খݶ ʢϓϥάΠϯͰ௥Ճʣ ޙํޓ׵ੑͷҡ࣋ Կ౓͔େ͖ͳมߋ͋Γ ଟগ͋Γ 3VCZͱͷؔ܎ 3VCZඪ४Ͱ͸ͳ͍ ཁHFNΠϯετʔϧ 3VCZඪ४όϯυϧ ·ͨ͸HFNΠϯετʔϧ 3BJMTͱͷؔ܎ ඪ४Ͱ͸ͳ͍ ཁηοτΞοϓ 3BJMTඪ४ 3BJMTνϡʔτϦΞϧͰ΋࠾༻ ຊମͷίʔυ ଟ͍ɺෳࡶ গͳ͍ɺγϯϓϧ ઃܭࢥ૝ ͢΂͕ͯୈҰڃΫϥε ΦϒδΣΫτ +VTU3VCZ
  15. RSpec ʹ͔͠ͳ͍ػೳ • ๛෋ͳϚονϟɺߴػೳͳϞοΫɺcontextɺ let!ɺshared examplesɺߦࢦఆ࣮ߦɺ υΩϡϝϯτܗࣜͷ݁Ռදࣔɺetc Minitest ʹ͔͠ͳ͍ػೳ •

    xUnit γϯλοΫεɺϕϯνϚʔΫςετɺ ฒྻ࣮ߦػೳɺϥϯμϜ࣮ߦ͕σϑΥϧτ ػೳͷൺֱ Minitest͸جຊతʹ ϓϥάΠϯͰରԠ
  16. RSpec vs Minitest 34QFD .JOJUFTU ར༻཰ɺ৘ใྔ ߴ͍ɺଟ͍ ௿͍ɺগͳ͍ σϑΥϧτͷػೳ ๛෋

    ඞཁ࠷খݶ ʢϓϥάΠϯͰ௥Ճʣ ޙํޓ׵ੑͷҡ࣋ Կ౓͔େ͖ͳมߋ͋Γ ଟগมߋ͋Γ
  17. RSpec • A.should → expect(A).to • be_true → be_truthy •

    have(n).items → ഇࢭɺผgemԽ • ଞଟ਺ Minitest • test/unit ޓ׵API → ഇࢭ (Ver. 5) • A.must_equal → _(A).must_equal (Ver. 6ʁ) • assert_template ౳ → ഇࢭɺผgemԽ (Rails 5) ޙํޓ׵ੑΛࣺͯͨมߋ Ͱ΋Transpec Ͱ ࣗಈม׵Ͱ͖Δ
  18. RSpec vs Minitest 34QFD .JOJUFTU ར༻཰ɺ৘ใྔ ߴ͍ɺଟ͍ ௿͍ɺগͳ͍ σϑΥϧτͷػೳ ๛෋

    ඞཁ࠷খݶ ʢϓϥάΠϯͰ௥Ճʣ ޙํޓ׵ੑͷҡ࣋ Կ౓͔େ͖ͳมߋ͋Γ ଟগ͋Γ 3VCZͱͷؔ܎ 3VCZඪ४Ͱ͸ͳ͍ ཁHFNΠϯετʔϧ 3VCZඪ४όϯυϧ HFNΠϯετʔϧ΋Մ 3BJMTͱͷؔ܎ 3BJMTඪ४Ͱ͸ͳ͍ ཁηοτΞοϓ 3BJMTඪ४ 3BJMTνϡʔτϦΞϧͰ΋࠾༻ ຊମͷίʔυ ଟ͍ɺෳࡶ গͳ͍ɺγϯϓϧ
  19. RSpec vs Minitest 34QFD .JOJUFTU ར༻཰ɺ৘ใྔ ߴ͍ɺଟ͍ ௿͍ɺগͳ͍ σϑΥϧτͷػೳ ๛෋

    ඞཁ࠷খݶ ʢϓϥάΠϯͰ௥Ճʣ ޙํޓ׵ੑͷҡ࣋ Կ౓͔େ͖ͳมߋ͋Γ ଟগ͋Γ 3VCZͱͷؔ܎ 3VCZඪ४Ͱ͸ͳ͍ ཁHFNΠϯετʔϧ 3VCZඪ४όϯυϧ ·ͨ͸HFNΠϯετʔϧ 3BJMTͱͷؔ܎ ඪ४Ͱ͸ͳ͍ ཁηοτΞοϓ 3BJMTඪ४ 3BJMTνϡʔτϦΞϧͰ΋࠾༻ ຊମͷίʔυ ଟ͍ɺෳࡶ গͳ͍ɺγϯϓϧ ύϑΥʔϚϯε ஗͍ʁʁʁ ଎͍ʁʁʁ
  20. ύϑΥʔϚϯε ͷҧ͍ʹ͍ͭͯ • Minitest ͸଎͞ΛചΓʹ͍ͯ͠Δ͕ɺ RSpec ͱମײͰ͖Δ΄Ͳͷࠩ͸ͳ͍ؾ͕͢Δ Minitest RSpec 1000ʙ49000݅΋

    ςετॻ͔ͳ͍͠… http://www.zenspider.com/pdf/2015_railsconf_ruby_on_rails_on_minitest.pdf ʁ
  21. ࣗ෼Ͱܭଌͯ͠Έͨ • Everyday RailsͷςετΛ RSpec όʔδϣϯͱ Minitest όʔδϣϯ(Specܗࣜ)Ͱ࣮ߦ • ςετͷ݅਺͸ͲͪΒ΋

    82݅ ʢ಺༰΋ಉ͡ʣ • 20ճ ࿈ଓ࣮ߦͯ͠ฏۉλΠϜΛࢉग़ ݁Ռ ɾ34QFD  ඵ ɾ.JOJUFTU  ඵ ͋ΕʁRSpecͷ ํ͕଎͔ͬͨ…
  22. ๻͕ RSpec ΛબͿཧ༝ • ҰݴͰ͍͏ͱɺଟػೳͰศར͔ͩΒ • ͢Ͱʹ RSpec ͷศརػೳΛϑϧ׆༻͍ͯ͠Δ ͷͰɺΘ͟Θ͟ػೳͷগͳ͍ϑϨʔϜϫʔΫʹ

    Ҡߦ͢ΔϝϦοτ͕ͳ͍ • ͨͩ͠ɺ୯७ͳςετͳΒ Minitest Ͱ΋OK • ηοτΞοϓ͕͍Βͳ͍ͷ͸େ͖ͳϝϦοτ
  23. Minitest ͰؾʹͳΔ఺ • ϓϥάΠϯΛଟ༻͢Δͱ ػೳͷিಥ ΍ ϝϯςφϯε໰୊ ͰࠔΓͦ͏ʢܦݧ͋Γʣ • ෳ਺ͷγϯλοΫε͕͋ΔͷͰɺϝϯόʔʹ

    Αͬͯ ޷Έ͕෼͔Εͦ͏ʢωοτͷ৘ใ΋όϥόϥʣ • RSpec ʹൺ΂Δͱ·ͩ·ͩ ৘ใ͕গͳ͍ ӳޠ ͕ۤखͳਓ͸ಛʹେม͔΋
  24. Minitest really is a testing framework for Do It Yourself-ers.

    Minitest͸ DIYer ͷͨΊͷςεςΟϯά ϑϨʔϜϫʔΫͰ͢ɻ The Minitest Cookbook http://chriskottom.com/minitestcookbook/ ͜ͷຊ͔ΒώϯτΛಘ·ͨ͠
  25. ๻Έ͍ͨͳλΠϓ͸ RSpec ޲͖ʁ • DIYerؾ࣭ ͕ͦΕ΄Ͳڧ͘ͳ͍ • gem ΍ϑϨʔϜϫʔΫ͸ ࢖͏ଆ

    ͕ϝΠϯ • DSL Λ࢖͏͜ͱʹ఍߅͕ͳ͍ɻΉ͠ΖDSLʹ ϝϦοτΛײ͍ͯ͡Δ • ଟػೳ ͳํ͕͍Ζ͍ΖϥΫग़དྷͯ޷͖