Created
July 4, 2012 17:53
-
-
Save macedo/3048586 to your computer and use it in GitHub Desktop.
RSpec matcher to test mass assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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