[rspec-users] Another attempt for a succinct model validation DSL
dchelimsky at gmail.com
Thu May 31 17:56:58 EDT 2007
On 5/31/07, nicholas a. evans <nick at ekenosen.net> wrote:
> On 5/28/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > The behaviour you're trying to describe is that the User should
> > require that passwords bear certain qualities - in this case a length
> > between 5 and 40. So you need to ask yourself how the User should
> > behave when this requirement is violated, and then specify that it
> > behaves that way.
> > Here's how I might handle this:
> > describe User do
> > it "should reject a password of length 4" do
> > User.create(:password => "1234").should have(1).error_on(:password)
> > end
> > it "should accept a password of length 5" do
> > User.create(:password => "12345").should have(0).errors_on(:password)
> > end
> That's pretty much exactly the pattern that I've been doing for
> specing my model validations... I like it; I think it is the
> simplest, most useful way of going about validations. But it can get
> a bit wordy, and the code duplication starts to become painful.
> I flooded the lists a few weeks back with some ideas for a nicer DSL
> for model validations, but I never actually followed up with a working
> implementation, because they all still felt a bit clunky to me... plus
> I haven't had any free weekends, nor have I been doing much with model
> validations at work recently.
> For some reason, looking at your example here made me want to try for
> a better DSL again. I like your use of the words "accept" and
> "reject" (versus "valid"/"invalid"), because they get right to the
> core of what actually happens, i.e. what the behavior is. And I like
> the emphasis on the examples (especially since I often think of
> TDD/BDD as EDD, Example Driven Design).
> So here's my latest thought-experiment: http://pastie.caboo.se/66478
> And also, a simpler multi-field version: http://pastie.caboo.se/66510
> Notice how checking all three sides of each boundary is so simple and
> succinct that there is almost no excuse not to do so. Also, after
> noticing how similar the multi-field validation is to a fit-table or
> some of Brian Marick and zenspider's recent blog entries, I decided
> that further simplification was necessary/desirable. :-)
> Consider this my request for comments on the syntax. Hopefully I'll
> have time to put my code where my mouth is and create a rspec
> extension to do this in the near future. :-)
Really interesting stuff. Thanks!
I took at a shot at something that feels more like the rest of RSpec.
It's admittedly more verbose than your examples, but I also think it
speaks well and aligns with everything else in rspec. WDYT?
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users