Mar
24
I using RSpec for testing my Ruby on Rails app.
For testing, if the correct attr_accessible are set, I use the following code.
My Class Media
class Media < ActiveRecord::Base attr_accessible :caption end
Usage in my media_spec.rb
it "should have protected attributes" do @media = Media.create!(@valid_attributes) @media.should accessible_attributes(:caption) end
Here the used code in lib/rspec_attributes_accessible.rb
module RspecAttributesAccessible
class AccessibleAttributes
def initialize(*attributes)
@attributes = attributes.to_a
end
def matches?(target)
@target = target
calculate_accessible_attributes()
perform_check()
end
def failure_message
"expected #{@failed_attribute} to be accessible"
end
def negative_failure_message
"expected #{@failed_attribute} to not be accessible"
end
private
def calculate_accessible_attributes()
attr_access = @target.class.send("attr_accessible").to_a.map(&:to_sym)
@failed_attribute = []
if((@attributes - attr_access).length != 0 or (attr_access - @attributes).length != 0)
@failed_attribute << (@attributes - attr_access)
@failed_attribute << (attr_access - @attributes)
end
nil
end
def perform_check()
@failed_attribute.empty?
end
end
def accessible_attributes(*attributes)
AccessibleAttributes.new(*attributes)
end
end
You have to include the lib in your spec_helper.rb
Spec::Runner.configure do |config| config.include RspecAttributesAccessible # lib/rspec_attributes_accessible.rb end
Useful links:

no comment untill now