On 27 Apr 2010, at 22:51, Patrick J. Collins wrote:

Hi,

What if they didn't? Is there a different way you could design this so that the interesting bits (The small methods) didn't depend so much on
the other bits around them?


Well this is for importing vCards...
http://gist.github.com/381384

So for example, I would like to just make a spec that does:

...

 before(:all) do
card_data = File.read(RAILS_ROOT + "/spec/fixtures/vcard/ kirk_no_photo.vcf")
   @vcard = Vcard.create!(:data => card_data)
   Contact.all.map(&:destroy)
 end

 describe "finding a contact" do

it "should find the contact when an email for the contact exists" do
     email = Email.create!(:address => "k...@enterprise.com")
contact = Contact.create!(:first_name => 'James', :last_name => 'Kirk')
     contact.emails << email

     @vcard.find_contact.should == contact
   end
 end

...


But, like I said, I am not quite sure how to structure it so that my @card ivar gets set..

Make them public. Move them to their own classes that encapsulate the
stuff they need to know about (e.g. the "setup".) When you test the
higher level methods you can mock this stuff. When you test the small
methods you should call them directly.

So is it bad practice then to use private methods? I have a habit of doing that-- but from what you're saying there is no way to test those things individually....

It's not bad practice to use private methods - lots of small methods in your classes is a good thing! As Adam says though, when a class gets hard to write microtests for, that's generally a sign that the design needs a re-think.

This subject has been discussed at length on this list before. Here's a good example:
http://groups.google.com/group/rspec/browse_thread/thread/9e3d879f712ce4f2/030af755918967dd


Patrick J. Collins
http://collinatorstudios.com
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

http://mattwynne.net
+447974 430184

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to