<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’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.<BR>
<BR>
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.<BR>
<BR>
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:<BR>
<BR>
should_belong_to<BR>
<BR>
should_require_attributes<BR>
<BR>
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.<BR>
<BR>
Is BDD even possible with Rails? (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’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:<BR>
<BR>
----------------------------------<BR>
<BR>
class Picture < ActiveRecord::Base<BR>
<BR>
end<BR>
<BR>
----------------------------------<BR>
<BR>
The first piece of code I would write if I <B>wasn’t</B> using BDD, would be:<BR>
<BR>
<BR>
----------------------------------<BR>
<BR>
class Picture < ActiveRecord::Base<BR>
<BR>
validates_as_attachment<BR>
<BR>
end<BR>
<BR>
----------------------------------<BR>
<BR>
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:<BR>
<BR>
----------------------------------<BR>
<BR>
describe Picture, ‘with a blank filename’ do<BR>
<BR>
before(:each) do<BR>
@picture = Picture.new valid_picture_attributes.except(:filename) # This uses some rSpec helpers<BR>
end<BR>
<BR>
it do<BR>
@picture.should_not be_valid<BR>
end<BR>
<BR>
end<BR>
<BR>
-----------------------------------<BR>
<BR>
So, the most simple way to solve that would be (this is part of what ‘validates_as_attachment’ does):<BR>
<BR>
----------------------------------<BR>
<BR>
class Picture < ActiveRecord::Base<BR>
<BR>
validates_presence_of :filename<BR>
<BR>
end<BR>
<BR>
----------------------------------<BR>
<BR>
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. <BR>
<BR>
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?<BR>
<BR>
Thanks,<BR>
<BR>
Matt Lins<BR>
</SPAN></FONT></FONT>
</BODY>
</HTML>