[rspec-users] RSpec makes me want to write better code

David Chelimsky dchelimsky at gmail.com
Fri Sep 26 18:10:16 EDT 2008

On Fri, Sep 26, 2008 at 12:18 PM, Mark Wilden <mark at mwilden.com> wrote:
> On Fri, Sep 26, 2008 at 9:47 AM, Ashley Moran
> <ashley.moran at patchspace.co.uk> wrote:
>> (be sure to spec what attributes your classes have if you're scared of
>> pollution!)
> As part of the TDD process, I spec all attributes, but this doesn't seem
> universal. Is this a misconception? Do people actually make sure that all
> columns exist and can be written to and read from?

To me, spec'ing attributes is a red flag. It is not always a bad thing
or wrong, but it suggests that the focus is on structure instead of
behaviour and results in a lot of unnecessary overhead in your code
examples, making them more brittle and more difficult to maintain.

For example, let's say we've got a Person model who can be old enough
to vote or not. I might start w/ a code example like this:

person = Person.new(:birthdate => 17.years.ago)
person.should_not be_voting_age

Then I'd do the following:

* run the example and discover that Person doesn't have a birthdate= method
* add and run a migration that adds birthdate
* run the example and discover that Person doesn't have a voting_age? method
* def voting_age?; false; end
* run the example and it passes
* add another example for the positive case
* etc, etc

In that process, adding and running the migration was the
implementation step. No need to spec the existence of the field
directly, because this example will fail without it. And the failure
message will be that the the Person doesn't respond to birthdate=, so
it's exactly the feedback I'd want because it would point me right to
the problem.

That all make sense?

Now sometimes there will be some up-front modeling discussions and you
may have a sense that a model needs a specific set of fields just
because that's what the customer says. In those cases, I'd recommend
trying to find something about the object's behaviour to rationalize
the presence of those fields.

Developer: why do we care about having a birthdate field?
Customer: because I want to know how old people are.
Developer: why? How will you find out?
Customer: we'll want to run demographics reports
Developer: why?
Customer: they'll help us sell advertising

OK, so now we have a feature, Demographics Reports, that warrants the
presence of a birthdate field, and it is in THAT context that the
birthdate field should come into existence.

Hope this all helps.


> ///ark
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list