Suggestion-for-RSpec-Custom-matcher

 Suggestion-for-RSpec-Custom-matcher

邦題: カスタムマッチャーを流行らせたい

RSpec のもろもろの協力機能のうちの一つである Custom matcher はとても良いのだけど思った以上にマイナーである。
それを何とかして流行らせたいので、紹介しました。

70e13d9877054026fda46d5a5b53a236?s=128

MOROHASHI Kyosuke

March 01, 2014
Tweet

Transcript

  1. Suggestion for 
 RSpec Custom Matcher ! ΧελϜϚονϟʔΛ ྲྀߦΒ͍ͤͨ MOROHASHI

    Kyosuke @2014-03-01 ԭೄRubyձٞ01
  2. ॾڮګհ(@moro) Kyosuke MOROHASHI

  3. None
  4. None
  5. None
  6. Suggestion for 
 RSpec Custom Matcher ! ΧελϜϚονϟʔΛ ྲྀߦΒ͍ͤͨ MOROHASHI

    Kyosuke @2014-03-01 ԭೄRubyձٞ01
  7. Ϣχοτςετʹ ͋Γ͕ͪͳ͜ͱ

  8. ✓ ࡉ͔͍ example ͕͍ͬͺ͍ ✓ example ͷू·Γ͔Βʮৼ Δ෣͍ʯΛ૝૾ͮ͠Β͍ ✓ ύϥϝλϥΠζਏ͍

  9. let(:job) { create(:job) } ! before do job.import_task_statuses( [:success, :doing,

    :success, :error] ) end ! specify { expect(job.tasks.where(status: :success)).to have(2).records } ! specify { expect(job.tasks.where(status: :doing)).to have(1).records }
  10. Rails doesn’t save YOUR Applications! (it’s an awesome FRAMEWORK!) http://sapporo.rubykaigi.org/2012/ja/schedule/details/79.html

    http://sapporo.rubykaigi.org/2012/ja/schedule/details/79.html
  11. Rails doesn’t save YOUR Applications! (it’s an awesome FRAMEWORK!) http://sapporo.rubykaigi.org/2012/ja/schedule/details/79.html

  12. Rails doesn’t save YOUR Applications! (it’s an awesome FRAMEWORK!) http://sapporo.rubykaigi.org/2012/ja/schedule/details/79.html

    also RSpec, too
  13. Test facilities for
 YOUR Applications.

  14. ✓ Arrange: Smart factory &/or
 Shared context ✓ Act: Your

    nice facade in 
 production code ✓ Assert: ?
  15. CustomMatcher ͷ ͝ఏҊ

  16. ΞϓϦέʔγϣϯ εέʔϧͷޠኮ

  17. RSpec::Matchers. define(:matchers_name) do |expected| match do |actual| actual == expected

    end end
  18. RSpec::Matchers. define(:matchers_name) do |expected| match do |actual| actual == expected

    end end
  19. ΞϓϦέʔγϣϯ εέʔϧͷޠኮ

  20. RSpec::Matchers. define(:have_task_statuses) do |expectation| ! match do |actual| statuses =

    actual.tasks.each_with_object(Hash.new( hash[t.status] += 1 } ! expect(statuses).to eq expectation end end
  21. let(:job) { create(:job) } ! before do job.import_task_statuses( [:success, :doing,

    :success, :error] ) end ! specify do expect(job).to have_task_statuses( success: 2, doing: 1, error: 1 ) end
  22. let(:job) { create(:job) } ! before do job.import_task_statuses( [:success, :doing,

    :success, :error] ) end ! specify { expect(job.tasks.where(status: :success)).to have(2).records } ! specify { expect(job.tasks.where(status: :doing)).to have(1).records } Before
  23. let(:job) { create(:job) } ! before do job.import_task_statuses( [:success, :doing,

    :success, :error] ) end ! specify do expect(job).to have_task_statuses( success: 2, doing: 1, error: 1 ) end After
  24. https://relishapp.com/rspec/rspec-expectations/v/3-0/docs/custom-matchers

  25. ·ͱΊ

  26. ✓ CustomMatcher؆୯Ͱ͠ΐ ✓ ͪΐͬͱͣͭҭͯΕ͹OK

  27. Rails doesn’t save YOUR Applications! (it’s an awesome FRAMEWORK!) http://sapporo.rubykaigi.org/2012/ja/schedule/details/79.html

    also RSpec, too
  28. ΞϓϦέʔγϣϯϩδοΫΛ ݕূ͢ΔͨΊͷޠኮΛ࡞Δͱ ΞϓϦέʔγϣϯϩδοΫΛ ݕূ͠΍͘͢ͳΔ

  29. http://martinfowler.com/articles/rubyAtThoughtWorks/metaprogramming.png