[rspec-users] How are people speccing Rails 3 ActiveRecord queries?

doug livesey biot023 at gmail.com
Sat Jul 17 12:47:45 EDT 2010


Interesting.
I'll give that a bash, too. I must admit, I tend to use the first approach,
but the second seems a little more true to the spirit of testing.
I can imagine that it might get rather involved when setting up the database
for even half-way complex queries, though.
I'll see how I get on with approach no. 2 for a while.
Thanks,
   Doug.

On 17 July 2010 17:11, David Chelimsky <dchelimsky at gmail.com> wrote:

> On Jul 17, 2010, at 10:37 AM, doug livesey wrote:
>
> > Hi -- how are people speccing Rails 3 ActiveRecord queries?
> > At the minute I'm chaining a load of should_receive calls on mock
> relation objects, but can't help thinking that there must be a more elegant
> way of going about it.
> > Is there a best practice for this, yet?
>
> For me, there is only one best practice: think. Everything else is a
> guideline in some context.
>
> That said, here are some of my guidelines, in context. YMMV.
>
> My approach to spec'ing models is different from that for controllers and
> views. In controller and view specs I prefer to stub out the data layer most
> of the time because I don't want the controller/view specs failing due to
> things like validations, which are model concerns.
>
> Spec'ing models is a different animal. I'll still use stubs where they make
> sense, but I'm perfectly happy using real objects and interacting with the
> database when spec'ing behavior that is influenced by the db and
> relationships between models.
>
> I prefer to think of spec'ing behavior rather than spec'ing query details.
> A simple example would be querying for the "active" members of a group. We
> could say:
>
> Member.should_receive(:where).with(:active => true)
> Member.active
>
> Or, we could say:
>
> active_member = Factory(:member, :active => true)
> inactive_member = Factory(:member, :active => false)
> Member.active.should eq([active_member])
>
> I, personally, find the latter more expressive, and we don't have to worry
> about the details of AR in the specs.
>
> HTH,
> David
>
> > Cheers,
> >    Doug.
>
> _______________________________________________
> 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/20100717/7b7f0ef7/attachment.html>


More information about the rspec-users mailing list