[rspec-users] Example for attr_accessible?

Pat Maddox pergesu at gmail.com
Wed Oct 17 00:51:52 EDT 2007

On 10/16/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> On 10/16/07, Pat Maddox <pergesu at gmail.com> wrote:
> > Personally, if I wanted to extract this pattern, I would use a custom
> > expectation matcher.
> ... which would probably be implemented like this under the hood:
> def matches?(target)
>   target.should_receive(:validates_presence_of).with(@expected)
>   load "#{RAILS_ROOT}/app/models/#{target.humanize}.rb"
> end
> No?

No.  I actually showed it in my post, though I suppose I did it in a
slightly confusing format.  I would implement it like so:

def matches?(target)
  instance = Target.new
  instance.should_not be_valid
  instance.error_messages.on(@expected).should == "can't be blank"

A couple key points:
1. AR defines validates_presence_of, but your subclass is responsible
for doing the actual validation
2. This particular code is lightweight enough that it's okay to use
the real implementation.  You don't get a big performance hit like you
do when you're creating/updating objects in the db and their
3. When you call MyModel.new when spec'ing business logic, it hits the
db to get the column info.  You already have a coupling to the DB
simply by using AR, so I don't think that's a good reason to prefer
expecting the validates_* method call.  If perf does become an issue,
you can stub out the columns and remove the db dependency all
together.  Jay Fields mentions that in a blog, and I think he's insane
(at least the way he showed), but it should be easy enough to preload
all the column info from schema.rb.  In fact I think I heard of that
technique from someone on this list.


More information about the rspec-users mailing list