[rspec-users] more rspec questions
adam.sroka at gmail.com
Thu Apr 29 01:51:07 EDT 2010
On Tue, Apr 27, 2010 at 4:56 PM, Patrick J. Collins
<patrick at collinatorstudios.com> wrote:
>> If something in the code you are testing depends on the return value
>> of a method then you would use a stub. e.g.:
> Right, but what I am asking is--- if all of my "slave methods" are relying on
> stored card data from the @card instance variable-- how is the best way to
> have access to the variable if it's only defined in the master method?
> if I have a method that does
> @card.addresses.each do |address|
> and I want to test that a contact does in fact get a new address created after
> that method is called with a vcard that has a valid address.
It might be that the problem is with the way you are stating your
test. It seems to me that the code you wrote above is the least
interesting part. We can just assume that iterating over addresses
works, it is what we do with them that is interesting. So that leaves
us with two ideas: 1) is an address that a vcard contains valid? 2)
can I add an address to a contact.
I would go a step further and say that adding an address to card is
not very interesting (Just like iterating over addresses, adding them
should work out of the box assuming Ruby itself isn't broken.)
So if the only interesting part of the above is that vcards can parse
addresses then there should be a public method for that which you test
by passing in a string and getting out an address object.
> I mean, one way to do this would be the getter/setter way-- have a card=/card
> Another way would be to just pass card into each method
> def process_addresses(card)
> .. But then I start feeling like my code is going to be less-elegant due to
> the fact that I am trying to write things that can be tested. Passing in a
> variable is definitely less elegant than using an instance variable, and a
> getter/setter is somewhat too (in my opinion).
I wouldn't say that it was a good idea to create a way to pass in a
card just for the sake of the test. However, I am presuming that at
some point you are going to want to do something with the cards.
Otherwise, why have them in the first place. So, if there is some
logic that gets the cards, and you have a Spec for that, then you will
need to set them somehow so that that Spec works.
More information about the rspec-users