[rspec-users] more rspec questions
adam.sroka at gmail.com
Tue Apr 27 18:35:08 EDT 2010
On Tue, Apr 27, 2010 at 2:51 PM, Patrick J. Collins
<patrick at collinatorstudios.com> wrote:
>> 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...
> 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)
> describe "finding a contact" do
> it "should find the contact when an email for the contact exists" do
> email = Email.create!(:address => "kirk at enterprise.com")
> contact = Contact.create!(:first_name => 'James', :last_name => 'Kirk')
> 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..
Based on the above I think the Vcard is a good opportunity for a mock.
That would most likely imply that you create the Vcard somewhere else
and pass it into this method. Also, you should directly test the
implementation of the Vcard outside of this spec (i.e. in it's own
>> 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....
No, it's not bad practice to use private methods. However, private
methods should mostly be helpers that encapsulate some small
calculation. If the method is interesting enough to be tested on its
own then it probably should be part of the public API. If the method
is not interesting enough to be tested on its own then it should be
private and tested through the public method that calls it.
As with anything, YMMV.
More information about the rspec-users