[rspec-users] Specify attr_protected

Luis Lavena luislavena at gmail.com
Wed May 30 20:04:08 EDT 2007


On 5/30/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> On 5/30/07, Jed Hurt <jed.hurt at gmail.com> wrote:
[...]
> > Question Two: I actually have a bunch of attributes that need to be
> > protected. Rather than hand-writing a call to the 'it' method for each
> > attribute, could I just loop over an array of attributes that need to
> > be checked and programatically define the 'it' calls?
> >
> > Pseudo-code:
> >
> > describe Order do
> >   [:total, :id, :customer_ip, :status, :error_message, :updated_at,
> > :created_at, :finalize, :tax, :shipping].each do |attribute|
> >     it "should protect #{attribute} attributes from mass assignment" do
> >       @order = Order.new(attribute => 'hax0rz')
> >       @order.attribute.should_not == 'hax0rz'
> >     end
> >   end
> > end
> >
> > What would the actual implementation look like?
>
> I think it would look exactly like what you wrote. Have you tried it?

I do exactly the same:

  [:title, :content_type, :basename].each do |attr|
    it { @asset.should have_present(attr) }
  end

  [:title, :basename].each do |attr|
    it { @asset.should have_unique(attr) }
  end

In this case, I use the self description provided by matchers, but
works also with #{} for double quoted strings.

-- 
Luis Lavena
Multimedia systems
-
Leaders are made, they are not born. They are made by hard effort,
which is the price which all of us must pay to achieve any goal that
is worthwhile.
Vince Lombardi


More information about the rspec-users mailing list