Skip to content

Instantly share code, notes, and snippets.

@macedo
Created July 4, 2012 17:53
Show Gist options
  • Save macedo/3048586 to your computer and use it in GitHub Desktop.
Save macedo/3048586 to your computer and use it in GitHub Desktop.
RSpec matcher to test mass assignment
RSpec::Matchers.define :allow_assignment_for do |attribute|
@attribute = attribute
match_for_should do |record|
@record = record
matches_against?(true)
end
match_for_should_not do |record|
@record = record
matches_against?(false)
end
def matches_against?(compare)
raise "The allow_assignment_for matcher requires an attribute; use subject.should allow_assignment_for(attribute)" unless @attribute
@record.class.accessible_attributes.include?(@attribute.to_sym) == compare
end
failure_message_for_should do |actual|
"expect to allow mass assignment for #{@attribute.inspect}"
end
failure_message_for_should_not do |actual|
"expect to reject mass assignment for #{@attribute.inspect}"
end
description do
"allow assignment for #{@attribute.inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment