[rspec-users] Four Question From an RSpec Baby - Give me something to chew

Nick Hoffman nick at deadorange.com
Wed Aug 27 14:25:54 EDT 2008

On 2008-08-27, at 13:20, Lake Denman wrote:
> For a visual guide:
>  def upgrade(payment)
>    transaction do
>      payment.user_id = self.id
>      payment.payment_type = Payment::SUBSCRIPTION_PAYMENT_TYPE
>      return false unless (payment.save and payment.external_id)
>      self.subscription_id = payment.external_id
>      self.payment_id = payment.id
>      self.member = true
>      self.member_since = AppLib.today_utc
>      self.save
>      return true
>    end
>  end

Hi Lake.

> 1.) Do I need to use any mocking/stubbing in this example (or in Unit
> Tests)

You need to use whatever you think is most suitable for your  
application. I'm sure many people on here would use mocks and stubs  
extensively throughout that method. For example, they might mock the  
entire "payment" object, and stub out AppLib.today_utc .

> 2.) Is it wrong to access multiple objects (user and payment in my
> example) in a Unit Test example?

I don't think so, but I'm not a unit test expert.

> 3.) Would you mind showing me an example of how you might implement a
> spec for this method.

User upgrade
- can succeed if the payment amount is correct
- fails if the payment amount is incorrect
- can succeed if the payment type is valid
- fails if the payment type if invalid

I'm new to RSpec though, so there's probably a "better"/clearer/more  
efficient way of doing it.

> 4.) Could you PLEASE PLEASE PLEASE guide me to a resource that helped
> you the most with figuring out Unit Testing with RSpec. Not limited to
> books or blog posts... good source code examples might be helpful.

Why not use BDD?  =)

> Hopefully you can see my sincere want to know more and I wish that a
> simple "want" will blossom into a realization of a "need" to test test
> test.

Just search, read, try, and ask.

> Thanks for any help you can provide me on my journey - I have a long  
> way
> ahead of me.

That doesn't answer all of your questions directly, but hopefully  
it'll be helpful in one way or another. Cheers,

More information about the rspec-users mailing list