[rspec-users] Testing attr_accessible (and/or attr_protected)

Alexey Ilyichev bluesman.alex at gmail.com
Tue Nov 2 02:33:18 EDT 2010


An attempt to assign protected attributes leads to the warning in your log
and not doing actual assignment.
You can test it like this:

describe Article, 'protected attributes' do
  it 'should deny mass-assignment to the user_id' do
    RAILS_DEFAULT_LOGGER.should_receive(:warn) # I ain't sure if it is
actually a warning, and I would just skip this check
    article.update_attributes(:person_id =>  @person.id)
    article.person_id.should == nil # or whatever it is before update call
  end
end

On Tue, Nov 2, 2010 at 3:36 AM, Iain E. Davis
<iain at somethingelectronic.com>wrote:

> I've been puzzling over how to test that attr_accessible has been set
> for the correct columns; but the tests I've come up with so far seem
> to fail to fail when I expect. I came across this old message from
> this list:
>
> http://www.mail-archive.com/rspec-users@rubyforge.org/msg01570.html
>
> Which seemed like a plausible example, but my attempt (modeled on the
> example) doesn't work:
>
> describe Article, 'protected attributes' do
>     it 'should deny mass-assignment to the user_id' do
>       lambda { article.update_attributes(:person_id =>  @person.id)
> }.should raise_error
>     end
> end
>
> The lambda doesn't raise an error, even though the attr_accessible
> doesn't include person_id.
>
> Where am I stumbling here? Is it my beginner's knowledge of rails, or
> beginner's knowledge of Ruby?
>
> Thanks,
>
> Iain
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101102/0e32cd8d/attachment.html>


More information about the rspec-users mailing list