[rspec-users] more rspec questions
matt at mattwynne.net
Tue Apr 27 18:45:21 EDT 2010
On 27 Apr 2010, at 22:51, Patrick J. Collins wrote:
>> What if they didn't? Is there a different way you could design this
>> that the interesting bits (The small methods) didn't depend so much
>> the other bits around them?
> Well this is for importing vCards...
> 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/
> @vcard = Vcard.create!(:data => card_data)
> describe "finding a contact" do
> it "should find the contact when an email for the contact exists"
> email = Email.create!(:address => "kirk at enterprise.com")
> contact = Contact.create!(:first_name => 'James', :last_name =>
> contact.emails << email
> @vcard.find_contact.should == contact
> 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:
> Patrick J. Collins
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users