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