Skip to content

Instantly share code, notes, and snippets.

@rodrigomaia
Created June 26, 2012 17:23
Show Gist options
  • Select an option

  • Save rodrigomaia/2997223 to your computer and use it in GitHub Desktop.

Select an option

Save rodrigomaia/2997223 to your computer and use it in GitHub Desktop.
How to create a RSpec Custom Matcher
#Matcher included_in
#my test
it "quando sortear, gravar os vencedores" do
concorrentes = Concorrente.where(:sorteio_id => @sorteio.id)
@sorteio.sortear
@sorteio.vencedores.should included_in(concorrentes)
end
#spec/support/spec_array_helper.rb
RSpec::Matchers.define :included_in do |expected|
match do |actual|
retorno = true
retorno = false if actual.empty? || expected.empty?
actual.each do |iten|
if not expected.map(&:id).include?(iten.try(:id))
retorno = false
break
end
end
retorno
end
failure_message_for_should do |actual|
"expected that #{actual} be included on #{expected}"
end
failure_message_for_should_not do |actual|
"expected that #{actual} would not be included on #{expected}"
end
description do
"be included on #{expected}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment