<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rspec code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 01:58:37 GMT</pubDate>
    <description>DZone Snippets: rspec code</description>
    <item>
      <title>RSpec extension: testing attr_protected and attr_accessible</title>
      <link>http://snippets.dzone.com/posts/show/4712</link>
      <description>Include this module in a spec or inside the Spec::Runner.configure block in the spec_helper.rb. Because i suck, should_not is not working as expected. Usage:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class User &lt; ActiveRecord::Base&lt;br /&gt;  # either &lt;br /&gt;  attr_accessible :username, :full_name&lt;br /&gt;  # or&lt;br /&gt;  attr_protected :admin, :foo&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# ...&lt;br /&gt;&lt;br /&gt;it "should protect admin and foo" do&lt;br /&gt;  @user.should protect_attributes(:admin, :foo)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here goes:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module CustomExpectations&lt;br /&gt;  class ProtectAttributes&lt;br /&gt;    def initialize(*attributes)&lt;br /&gt;      @attributes = attributes&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    def matches?(target)&lt;br /&gt;      @target = target&lt;br /&gt;      &lt;br /&gt;      calculate_protected_methods&lt;br /&gt;      perform_check&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    def failure_message&lt;br /&gt;      "expected #{@failed_attribute} to be protected"&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    def negative_failure_message&lt;br /&gt;      "expected #{@failed_attribute} to not be protected"&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    private&lt;br /&gt;    &lt;br /&gt;    def calculate_protected_methods&lt;br /&gt;      read = proc {|var| @target.instance_eval { self.class.read_inheritable_attribute(var) } }&lt;br /&gt;      accessible = read.call("attr_accessible")&lt;br /&gt;      protekted = read.call("attr_protected")&lt;br /&gt;      all = @target.class.column_names.map(&amp;:to_sym)&lt;br /&gt;      &lt;br /&gt;      @protected = []&lt;br /&gt;      @protected &lt;&lt; protekted if protekted&lt;br /&gt;      @protected &lt;&lt; (all - accessible) if accessible&lt;br /&gt;      @protected.flatten!&lt;br /&gt;      &lt;br /&gt;      @accessible = all - @protected&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    def perform_check&lt;br /&gt;      failed_attributes = (@attributes &amp; @accessible)&lt;br /&gt;      @failed_attribute = failed_attributes.first&lt;br /&gt;      failed_attributes.empty?&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def protect_attributes(attributes)&lt;br /&gt;    ProtectAttributes.new(attributes)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def protect_attribute(attribute)&lt;br /&gt;    ProtectAttributes.new(*[attribute])&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 29 Oct 2007 20:28:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4712</guid>
      <author>leethal (August Lilleaas)</author>
    </item>
  </channel>
</rss>
