Skip to content

Instantly share code, notes, and snippets.

@fnando
Created July 14, 2011 02:00
Show Gist options
  • Save fnando/1081807 to your computer and use it in GitHub Desktop.
Save fnando/1081807 to your computer and use it in GitHub Desktop.
RSpec matcher
RSpec::Matchers.define :allow do |*values|
match do |record|
values.collect {|value|
record.send("#{@attribute}=", value)
record.valid?
record.errors[@attribute].empty?
}.all?
end
chain :as do |attribute|
@attribute = attribute
end
failure_message_for_should do |record|
"expected #{record.inspect} to allow each of #{values.inspect} as #{@attribute.inspect} value"
end
failure_message_for_should_not do |record|
"expected #{record.inspect} to reject each of #{values.inspect} as #{@attribute.inspect} value"
end
description do
"allow #{values.inspect} values for #{@attribute.inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment