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

Suggestion-for-RSpec-Custom-matcher

 Suggestion-for-RSpec-Custom-matcher

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

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

MOROHASHI Kyosuke

March 01, 2014
Tweet

More Decks by MOROHASHI Kyosuke

Other Decks in Programming

Transcript

  1. 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 }
  2. ✓ Arrange: Smart factory &/or
 Shared context ✓ Act: Your

    nice facade in 
 production code ✓ Assert: ?
  3. 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
  4. 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
  5. 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
  6. 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