[rspec-users] BDD/Rails/Shoulda

Jonathan Linowes jonathan at parkerhill.com
Thu Apr 24 13:58:06 EDT 2008


I'm not sure this answers your questions, but you prompted me to  
share my experience.

Personally i consider BDD just one tool in my toolbox. And I consider  
rspec to be as much a testing tool as a (BD)Development one. So I  
often find myself just taking the path of least resistance. And  
iterating.

In some cases it really 'feels right' to write the examples first and  
then implement the code, repeat... I love doing that, the 'BDD way'  
is fun.

But half the time, I find myself working 'the old fashioned way' - I  
write down (often with pen and paper, omg!) a list of things my code  
needs to do, I implement it, test it manually in the browser or  
console. After a point (not too much later though) I then go back and  
write specs that verify my code and firm up my intent. I think this  
is because Ruby and Rails can be so expressive in themselves (like  
the plugins you mentioned). I've jokingly referred to this as DDB  
(development driven behavior).

Importantly, when I code first, then spec, the spec phase is not an  
'afterthought'. Rather, the code can be thought of a first pass or  
prototype, and the specs get me the firm up (or reevaluate) the  
behavior and/or refactor my code. Bottom line -- the process is  
iterative.

In the end, both approaches leave me with quality code, expressive  
specs, edge test cases, and regression tests.

Obviously I'm not speaking as a BDD priest, rather as a soldier in  
the trenches.

linoj


On Apr 24, 2008, at 12:30 PM, Matthew Lins wrote:

> This is a little off topic, but I use rSpec and I’m starting to  
> question the quality of my specs.  In my research and attempt to  
> learn how to write better specs, I’ve came across a few things that  
> I’d like to discuss.
>
> I’m having more and more difficulty understanding BDD.  The more I  
> read and the more I watch, the more questions I come up with.   Let  
> me just ask a couple of general questions to start this off.
>
> Is ‘shoulda’ actually following the principals of BDD?  But, I  
> guess that’s not really a good question.  Is ‘shoulda’ encouraging  
> it’s users to follow the principals of BDD?  I see all of the  
> macros like:
>
> should_belong_to
>
> should_require_attributes
>
> To me, that is not BDD.  Basically that’s just testing whether or  
> not your model contains a certain code fragment.  But, that brings  
> me to my next question.
>
> Is BDD even possible with Rails?  (I think it is, but I ask myself  
> that more and more lately)
>
> I just picked a random model in the application I’m currently  
> working on.  The ‘Picture’ model.  I use the attachment_fu plugin,  
> which helps this model handle pictures (it creates thumbnails,  
> validates sizes, etc.)  I wiped out all the code I had and all the  
> specs I had.  I started from scratch:
>
> ----------------------------------
>
> class Picture < ActiveRecord::Base
>
> end
>
> ----------------------------------
>
> The first piece of code I would write if I wasn’t using BDD, would be:
>
>
> ----------------------------------
>
> class Picture < ActiveRecord::Base
>
>   validates_as_attachment
>
> end
>
> ----------------------------------
>
> Which basically handles all of my validation.  So, from a BDD  
> perspective, how do I spec that?  I know, I know, I should be  
> writing the specs first.  But, what do I do about these helpers  
> that come with plugins.  Do I write a spec:
>
> ----------------------------------
>
> describe Picture, ‘with a blank filename’ do
>
>   before(:each) do
>     @picture = Picture.new valid_picture_attributes.except 
> (:filename) # This uses some rSpec helpers
>   end
>
>   it do
>     @picture.should_not be_valid
>   end
>
> end
>
> -----------------------------------
>
> So, the most simple way to solve that would be (this is part of  
> what ‘validates_as_attachment’ does):
>
> ----------------------------------
>
> class Picture < ActiveRecord::Base
>
>   validates_presence_of :filename
>
> end
>
> ----------------------------------
>
> But, now what, I’m going to reverse engineer this plugin’s helper?   
> I’ll just spec it all out and eventually refactor and put the  
> ‘validates_as_attachment’ back?  Or, maybe since this is a plugins  
> helper I don’t even need to test any of this.  It’s the author of  
> the plugin’s responsibility.  This is were my brain enters an  
> infinite loop (one example anyway, hehe).  I just can’t seem to  
> nail down the workflow when specing rails apps.  I also have a hard  
> time determining what to spec.
>
> I know I asked a lot of questions, but basically I’m just trying to  
> find out if people are actually following the BDD principals  
> strictly when writing Rails apps.  If you are can you give me some  
> insight in the above example?
>
> Thanks,
>
> Matt Lins
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080424/9415e2cb/attachment-0001.html 


More information about the rspec-users mailing list