[rspec-users] Best practices: How small to make examples?

David Chelimsky dchelimsky at gmail.com
Sun Apr 5 08:20:34 EDT 2009

On Sun, Apr 5, 2009 at 4:24 AM, Matt Wynne <matt at mattwynne.net> wrote:
> On 5 Apr 2009, at 03:56, Brandon Olivares wrote:
>> Hi,
>> How small should examples be?
>> Let's say I have a form, should there be one example per field? If there's
>> a
>> dropdown, should I describe the dropdown and have one example per option?
>> I
>> thought that at first but now, because of the duplication and sheer
>> verbosity, it seems too much.
>> So what do you think?
> http://blog.jayfields.com/2007/06/testing-one-assertion-per-test.html

or http://rubyurl.com/yCt3

> Again, I would urge you to listen to the tests and question whether writing
> unit tests for your views is worthwhile. If you're finding it a hassle,
> there might be a reason for that :)

There is a trend these days away from isolated view specs. My own
opinion is that this is *generally* a good thing *if* you've got
something like cucumber scenarios that take you through the views in

That said, I'm not yet convinced that view specs shouldn't still be
part of your toolkit. I generally use RSpec and Cucumber these days,
and I like to progress in small, verifiable steps. So let's say I set
up a cucumber scenario in the declarative style:

Given I register for a conference
Then my name should be on the list of conference attendees
And the conference should be my list of conferences

Note that lack of "When I fill in ..." sort of steps. The first given
is going to take care of the details of filling in the form. This way,
if something about the form changes, I need to change the underlying
step definition, but I don't need to revisit all the scenarios that
use it. So lets say the form has 10 fields split out into two pages.
That's going to take some time to get working, and the whole time I'm
developing the parts, I'm going to have this one failing step.

In a case like this, I might spec the view using rspec, so that I can
add one field at a time and verify each field as its done. This way
I'm getting green bars every minute or so, which keeps me relaxed as I

But that's me. And I don't do it or recommend doing it for everything
all the time. Paraphrasing Matt, "listen to your specs."


> Matt Wynne
> http://blog.mattwynne.net
> http://www.songkick.com
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list