[rspec-users] testing behaviour or testing code?
Jay Levitt
lists-rspec at shopwatch.org
Fri Sep 7 11:51:19 EDT 2007
Wincent Colaiuta wrote:
> El 7/9/2007, a las 5:36, Jay Levitt escribió:
>
> Very true that you shouldn't be testing ActiveRecord's validation
> (Rails' own unit tests are there for that).
>
> But if you want to do truly *driven* BDD then you will have to test
> something; in other words, *before* you go ahead and add this line to
> your model:
>
> validates_presence_of :foo
>
> You need to write a failing spec for it first. Otherwise, why would
> you write it? Doing BDD in its purest form you shouldn't be writing
> *any* line of code without your specs driving it. This means the
> familiar "write failing spec, write code, confirm working spec" cycle.
>
> So the question is, what is the best kind of spec to write to *drive*
> the writing of your "validates_presence_of" lines? For some
> validations it's quite easy. For others it is less straightforward.
> There are probably multiple valid (or valid-ish) answers, but it's
> sometimes difficult to know which one is best.
Well put! To me, if the spec I write is:
Model.expects(:validates_presence_of).with(:digits)
then I haven't written a spec at all - I've written the code I plan to
write, and spelled it differently! The English version of that spec is:
Model
- should call validates_presence_of with parameter :digits
That's just specifying what a line of my code should *say*, not how
Model should *behave*.
I really like Wincent's approach - test that valid input yields a valid
response and that invalid input yields an invalid response.
Jay
More information about the rspec-users
mailing list