[rspec-users] have_one and have_present

nicholas a. evans nick at ekenosen.net
Sat May 5 10:45:18 EDT 2007

On 5/4/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> FYI - Jay Fields has a Validatable framework that includes some
> test/unit assertions that look like this:
> Foo.must_validate do
>   presence_of :name
>   format_of(:name).with(/^[A-Z]/)
>   numericality_of(:age).only_integer(true)
> end
> Whether you choose to use his framework or write your own, I think
> there is something to be learned from its expressiveness.

Something doesn't sit right with me for both Jay Field's test
validations and also

To me, it feels like both of them are testing the implementation too
closely, rather than checking the expected behavior.  I think that
this problem is best exhibited by format_of.  What am I verifying?
That the production code contains the exact same regex as my spec
code?  That seems a bit silly to me, when the regex itself is the item
that should be tested most!  It's what is carrying the behavior.  And,
while the simple "first character should be a capital letter" may seem
like a silly one to worry about, what about a more complicated regex,
such as an email address validation?  I would seriously distrust any
spec containing a complicated regex which is attempting to validate
another complicated regex.

What I'd *really* like is something a lot simpler (even if more
verbose), that focuses entirely on examples and the behavior exhibited
for those examples.  Basically, I want to give a set of invalid
examples, and verify that they are indeed invalid, as well as some
valid examples, and verify that they pass validation.

But I'm still having a hard time coming up with a nice syntax for
this.  I was having a discussion with a coworker about this earlier in
the week, and I pastied my first thoughts about this here:
http://pastie.caboo.se/58438  After resting on it a bit, I also came
up with another syntax, pastied here: http://pastie.caboo.se/59162

I'll readily admit that Jay Field's DSL looks much nicer than what
I've come up with so far... but I'd rather stick with the plain
vanilla rspec, and be certain that I'm asserting an expected model
behavior against examples than expect the implementation details of
which ActiveRecord validation was used.

Am I just making things harder for myself?  Am I missing the boat on a
useful simplification?


More information about the rspec-users mailing list