« Return to Thread: BDD/Rails/Shoulda

BDD/Rails/Shoulda

by Matthew Lins :: Rate this Message:

Reply to Author | View in Thread

BDD/Rails/Shoulda 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@...
http://rubyforge.org/mailman/listinfo/rspec-users

 « Return to Thread: BDD/Rails/Shoulda