Pro Yearly is on sale from $80 to $50! »

RSpecと非同期関数テスト

 RSpecと非同期関数テスト

於 TokyuRubyKaigi11 2017/07/29
https://tokyurubykaigi.github.io/tokyu11/

4f8c3a1aedaf9aafd6c74ab077d9ad18?s=128

shigeru. nakajima

July 29, 2017
Tweet

Transcript

  1. Rspecͱඇಉظؔ਺ςετ גࣜձࣾϥάβΠΞ தౡ ࣎ @TokyuRubyKaigi 2017/07/29

  2. ࣗݾ঺հ → தౡ ࣎ a.k.a. ledsun → डୗ։ൃͰWebΞϓϦέʔγϣϯΛ։ൃ → Railsܦݧ൒೥

  3. ࠷ॳʹେࣄͳ͜ͱ → RSpecͰඇಉظؔ਺Λςετ͢Δ ྑ͍ํ๏Λڭ͑ͯཉ͍͠ → RSpecͰͳͯ͘΋RubyͰ ඇಉظؔ਺Λςετ͢Δ...

  4. Θ͔͍ͬͯΔ͜ͱ → RSpec͸ςετࣦഊͷऩूʹྫ֎Λ࢖͏ → RubyͰ͸αϒεϨου಺ͷྫ֎͸ ͕Μ͹Βͳ͍ͱั·͑ΒΕͳ͍

  5. ೰ΜͰ͍Δ͜ͱ ඇಉظؔ਺ ಛʹεϨουΛ࢖࣮ͬͯ૷ Ͳ͏ςετ͢Δʁ

  6. ςετ͍ͨؔ͠਺ͷ࣮૷Πϝʔδ def async_func Thread.new do # Կ͔͕͔͔࣌ؒΔॲཧ yield result end

    end async_func do |result| # ݁ՌΛ΋ΒͬͯԿ͔͢Δ end
  7. ΍Γ͍ͨςετ it 'ඇಉظؔ਺ͷࣦഊΛั·͑ΒΕΔ͜ͱ' do async_func do |result| expect(result).to eq(expected_value) #expectͷࣦഊ

    => ྫ֎ end end async_funcͷத͸ผεϨου ผεϨουͷྫ֎ΛΩϟονͰ͖ͳ͍
  8. ࠔΔ͜ͱ expectࣦഊΛݕ஌Ͱ͖ͳ͍

  9. ϝΠϯεϨουʹྫ֎Λ౉͢ʹ͸ʁ → εϨουม਺Λฦͯ͠ ݺͼग़͠ݩͰjoin → ྫ֎Ωϟονͯ͠ ϝΠϯεϨουʹ޲͔ͬͯraise

  10. ϓϩμΫτίʔυʹಛఆͷ࣮૷Λڧ੍͢Δʁ → ϓϩμΫτίʔυͷ࣮૷ϛεͬͨΒʁ → ݕ஌Ͱ͖ͳ͍ → ςετίʔυͷҙຯʁ

  11. Ͱ͖ͦ͏ͳ͜ͱ → RSpecͷࣦഊऩूػߏΛྫ֎ํ͔ࣜΒॻ͖׵͑Δ failure_notifierลΓ → ඇಉظؔ਺ΛεϨουҎ֎Ͱॻ͖௚͢ Fiberʁ → ඇಉظؔ਺ͷ݁ՌΛϝΠϯεϨουʹ໭͢ ϝΠϯεϨουͰexpect

  12. Ͱ͖ͦ͏ʁ RSpecʹखΛೖΕΔͷ͸େมͦ͏ FiberΑ͘Θ͔Βͳ͍ɺ͜Θ͍

  13. ඇಉظؔ਺ͷ݁ՌΛϝΠϯεϨουʹ໭͢ it 'ඇಉظؔ਺ͷࣦഊΛั·͑ΒΕΔ͜ͱ' do result = async_play do |curtail| async_func

    do |result| curtail.call result end end expect(result).to eq(expected_value) end ͜ΕͳΒςετͰ͖ͦ͏
  14. ΩϡʔΛ࢖͏ def async_play q = Queue.new yield (reuslt) -> {

    q.push result } q.pop end Ωϡʔ͸pop͢Δͱ push͞ΕΔ·Ͱ଴ͭ
  15. ࢼ͔ͨͬͨ͠Βgem͋Δ gem 'async_play' results = AsyncPlay.opening{ | curtain | Thread.new

    { curtain.call 1 } } ৄࡉ͸ http://qiita.com/ledsun/items/ 0e1dd4ece43dc56653c7
  16. ΋ͬͱྑ͍ํ๏͕͋Ε͹ ڭ͍͑ͯͩ͘͞