Junichi Ito
July 11, 2015
22k

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

http://regional.rubykaigi.org/kansai06/

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

July 11, 2015

## Transcript

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

Twitter: @jnchito • RailsϓϩάϥϚ • େࡕ෎๛தࢢग़਎ • ฌݿݝ੢࿬ࢢࡏॅ

12. ### ͜Ε͔Β͓࿩͢͠Δ಺༰ • ςεςΟϯάϑϨʔϜϫʔΫͷ໾ׂ • RSpec ͱ Minitest ͷ؆୯ͳίʔυྫ • γϯλοΫεͷൺֱʢxUnitܗࣜ

vs Specܗࣜʣ • ϑϨʔϜϫʔΫͷൺֱʢRSpec vs Mintiestʣ • ߟ࡯ɿRSpec ͸ͳͥҒ͍ਓʹ޷͔Εͳ͍ͷ͔ʁ • ·ͱΊɿ࢖͏ͳΒͲͬͪʁ

14. ### ςεςΟϯάϑϨʔϜϫʔΫͷ໾ׂ • ςετίʔυΛ࣮ߦ͠ɺظ଴ͨ͠௨Γʹ ϓϩάϥϜ͕ಈ࡞͢Δ͜ͱΛݕূ͢Δ • ςετίʔυͱ͸ → ϓϩάϥϜΛςετ͢ΔϓϩάϥϜ • ݕূ݁Ռʢ੒ޭ

or ࣦഊʣΛ։ൃऀʹใࠂ͢Δ

16. ### ςετର৅ͷϓϩάϥϜ class Greeter def say 'Hello, world!' end end ɾGreeter

Ϋϥε ɾsay ϝιου ɾ‘Hello, world!’ ɹΛฦ͢
17. ### ্͕ 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ܗࣜ
18. ### 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 ʹͦͬ͘Γʂʂ

Minitest

21. ### xUnitܗࣜ vs Specܗࣜ Y6OJUܗࣜ 4QFDܗࣜ γϯλοΫε ϐϡΞ3VCZ %4-ʢυΩϡϝϯτ෩ʣ शಘͷϋʔυϧ 3VCZͷจ๏͕Θ͔Ε͹0,

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

= age end def greet if @age <= 12 "΅͘͸#{@name}ͩΑɻ" else "๻͸#{@name}Ͱ͢ɻ" end end end ৚݅ʹԠͯ͡ςετίʔυ Λߏ଄Խ͍ͨ͠
23. ### 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 ϒϩοΫͰάϧʔϓԽ (ߏ଄Խ)
24. ### 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 ݺΜͩΓ…
25. ### Ͱ΋ߏ଄Խ͠ͳ͍ํ͕γϯϓϧ 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 ͥΜͿϑϥοτͰ͑͑΍Μ
26. ### xUnitܗࣜ vs Specܗࣜ Y6OJUܗࣜ 4QFDܗࣜ γϯλοΫε ϐϡΞ3VCZ %4-ʢυΩϡϝϯτ෩ʣ शಘͷϋʔυϧ 3VCZͷจ๏͕Θ͔Ε͹0,

%4-Λ֮͑Δඞཁ͋Γ ϩδοΫͷࣗ༝౓ 3VCZͷݴޠػೳΛ ϑϧ׆༻Ͱ͖Δ %4-ʹ੍໿͞Ε΍͍͢ ݕূϝιου BTTFSU@FRVBM" # FYQFDU # UPFR" ·ͨ͸#NVTU@FRVBM" ςετͷߏ଄Խ Ϋϥεͷܧঝ·ͨ͸ ແཧʹߏ଄Խ͠ͳ͍ ϒϩοΫΛωετͤ͞Δ ໋໊ͷࣗ༝౓ Ϋϥε໊΍ϝιου໊ͷ ॏෳʹ஫ҙ͕ඞཁ ໋໊͸ൺֱతࣗ༝

28. ### xUnitܗࣜ vs Specܗࣜ Y6OJUܗࣜ 4QFDܗࣜ γϯλοΫε ϐϡΞ3VCZ %4-ʢυΩϡϝϯτ෩ʣ शಘͷϋʔυϧ 3VCZͷจ๏͕Θ͔Ε͹0,

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

30. ### RSpec vs Minitest 34QFD .JOJUFTU ར༻཰ɺ৘ใྔ ߴ͍ɺଟ͍ ௿͍ɺগͳ͍ σϑΥϧτͷػೳ ๛෋

ඞཁ࠷খݶ ʢϓϥάΠϯͰ௥Ճʣ ޙํޓ׵ੑͷҡ࣋ Կ౓͔େ͖ͳมߋ͋Γ ଟগ͋Γ ఏڙܗଶ 3VCZඪ४Ͱ͸ͳ͍ ཁHFNΠϯετʔϧ 3VCZඪ४όϯυϧ ·ͨ͸HFNΠϯετʔϧ 3BJMTͱͷؔ܎ ඪ४Ͱ͸ͳ͍ ཁηοτΞοϓ 3BJMTඪ४ 3BJMTνϡʔτϦΞϧͰ΋࠾༻ ຊମͷίʔυ ଟ͍ɺෳࡶ গͳ͍ɺγϯϓϧ ઃܭࢥ૝ ͢΂͕ͯୈҰڃΫϥε ΦϒδΣΫτ +VTU3VCZ

275 280 3 15
32. ### Qiitaͷ౤ߘ਺ɺϑΥϩϫʔ਺ ౤ߘ਺ = 473 ϑΥϩϫʔ = 294 ౤ߘ਺ = 12

ϑΥϩϫʔ = 5 ৘ใྔ΋ଟ͍
33. ### RSpec vs Minitest 34QFD .JOJUFTU ར༻཰ɺ৘ใྔ ߴ͍ɺଟ͍ ௿͍ɺগͳ͍ σϑΥϧτͷػೳ ๛෋

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

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

ඞཁ࠷খݶ ʢϓϥάΠϯͰ௥Ճʣ ޙํޓ׵ੑͷҡ࣋ Կ౓͔େ͖ͳมߋ͋Γ ଟগมߋ͋Γ
36. ### 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 Ͱ ࣗಈม׵Ͱ͖Δ
37. ### RSpec vs Minitest 34QFD .JOJUFTU ར༻཰ɺ৘ใྔ ߴ͍ɺଟ͍ ௿͍ɺগͳ͍ σϑΥϧτͷػೳ ๛෋

ඞཁ࠷খݶ ʢϓϥάΠϯͰ௥Ճʣ ޙํޓ׵ੑͷҡ࣋ Կ౓͔େ͖ͳมߋ͋Γ ଟগ͋Γ 3VCZͱͷؔ܎ 3VCZඪ४Ͱ͸ͳ͍ ཁHFNΠϯετʔϧ 3VCZඪ४όϯυϧ HFNΠϯετʔϧ΋Մ 3BJMTͱͷؔ܎ 3BJMTඪ४Ͱ͸ͳ͍ ཁηοτΞοϓ 3BJMTඪ४ 3BJMTνϡʔτϦΞϧͰ΋࠾༻ ຊମͷίʔυ ଟ͍ɺෳࡶ গͳ͍ɺγϯϓϧ

39. ### RSpec vs Minitest 34QFD .JOJUFTU ར༻཰ɺ৘ใྔ ߴ͍ɺଟ͍ ௿͍ɺগͳ͍ σϑΥϧτͷػೳ ๛෋

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

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

82݅ ʢ಺༰΋ಉ͡ʣ • 20ճ ࿈ଓ࣮ߦͯ͠ฏۉλΠϜΛࢉग़ ݁Ռ ɾ34QFD  ඵ ɾ.JOJUFTU  ඵ ͋ΕʁRSpecͷ ํ͕଎͔ͬͨ…

47. ### ๻͕ RSpec ΛબͿཧ༝ • ҰݴͰ͍͏ͱɺଟػೳͰศར͔ͩΒ • ͢Ͱʹ RSpec ͷศརػೳΛϑϧ׆༻͍ͯ͠Δ ͷͰɺΘ͟Θ͟ػೳͷগͳ͍ϑϨʔϜϫʔΫʹ

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

Αͬͯ ޷Έ͕෼͔Εͦ͏ʢωοτͷ৘ใ΋όϥόϥʣ • RSpec ʹൺ΂Δͱ·ͩ·ͩ ৘ใ͕গͳ͍ ӳޠ ͕ۤखͳਓ͸ಛʹେม͔΋

64. ### DIYerͬͯԿʁ • DIYer = Do It Yourself-er • ʮͦͷػೳɺͳ͍ͳΒࣗ෼Ͱ࡞Δͥʂʯͱ ߟ͑Δਓ

• ͻ͍ͯ͸gem΍ϑϨʔϜϫʔΫΛΑ͘ ࡞͍ͬͯΔਓ
65. ### Minitest really is a testing framework for Do It Yourself-ers.

Minitest͸ DIYer ͷͨΊͷςεςΟϯά ϑϨʔϜϫʔΫͰ͢ɻ The Minitest Cookbook http://chriskottom.com/minitestcookbook/ ͜ͷຊ͔ΒώϯτΛಘ·ͨ͠
66. ### DIYerؾ࣭ ͕ڧ͍ਓ΄Ͳɾɾɾ • ԟʑʹͯ͠RubyͷΤΩεύʔτͳͷͰɺDSLΛ ֮͑Δ͙Β͍ͳΒϐϡΞRubyͰॻ͍ͨํ͕଎͍ • ࣗ෼Ͱ޷͖ͳΑ͏ʹ֦ு͍ͨ͠ͷͰɺgemͷ ίʔυ͸ খͯ͘͞ಡΈ΍͍͢ ํ͕๬·͍͠

• APIઃܭʹڧ͍ͩ͜ΘΓͱ৴೦͕͋ΔͷͰ ഽʹ߹Θͳ͍γϯλοΫε ͸࢖͍ͨ͘ͳ͍

69. ### ๻Έ͍ͨͳλΠϓ͸ RSpec ޲͖ʁ • DIYerؾ࣭ ͕ͦΕ΄Ͳڧ͘ͳ͍ • gem ΍ϑϨʔϜϫʔΫ͸ ࢖͏ଆ

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

71. ### RSpecͱMinitestɺ࢖͏ͳΒͲͬͪʁ • Ұൠ࿦Ͱ͍͑͹ɺͲͬͪͰ΋ྑ͍ʢ౰ͨΓલʣ • ௕ॴ୹ॴ͸ͲͪΒʹ΋͋Δ • ݸਓతͳΦεεϝʢ๻ͷ޷Έʣ͸ RSpec • ͨͩ͠ɺDIYerؾ࣭͕ڧ͍ਓ΍ɺ

ͱʹ͔͘ RSpec ͕ݏ͍ʂͱ͍͏ਓ͸আ͘ • Ͱ΋ࣗ෼Ͱ࢖͔ͬͯ֬ΊΔͷ͕ϕετͰ͢ʂ