[rspec-users] testing behaviour or testing code?

Pat Maddox pergesu at gmail.com
Fri Aug 24 13:06:11 EDT 2007

On 8/24/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> describe Widget, "class" do
>   it "should provide a list of widgets sorted alphabetically" do
>     Widget.should_receive(:find).with(:order => "name ASC")
>     Widget.find_alphabetically
>   end
> end
> You're correct that the refactoring requires you to change the
> object-level examples, and that is something that would be nice to
> avoid. But also keep in mind that in java and C# people refactor
> things like that all the time without batting an eye, because the
> tools make it a one-step activity. Refactoring is changing the design
> of your *system* without changing its behaviour. That doesn't really
> fly all the way down to the object level 100% of the time.

I think that example is fine up until the model spec.  The
find_alphabetically example should hit the db, imo.  With the current
spec there's no way to know whether find_alphabetically actually works
or not.  You're relying on knowledge of ActiveRecord here, trusting
that the arguments to find are correct.

What I've found when I write specs is that I discover new layers of
services until eventually I get to a layer that actually does
something.  When I get there, it's important to have specs that
describe what it does, not how it does it.  In the case of
find_alphabetically we care that it returns the items in alphabetical
order.  Not that it makes a certain call to the db.


More information about the rspec-users mailing list