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

Lake Denman lists at ruby-forum.com
Wed Aug 27 13:20:06 EDT 2008


The project has been written - around 10,000 lines of code, but
certainly less than that to add tests to. There has been no official
testing put into place, and I'd very much like to implement RSpec into
the project for a few reasons, but mostly to setup solid examples for
the behavior of the application.

It was suggested to me to begin with Unit Tests. The User Model is
almost 1000 lines with plenty of methods and no coverage, so I figured I
would start there. After installing Rcov to track my progress, I wrote
about 10 passing examples that were, I guess, pretty trivial - the
methods I tested all dealt directly with an "instantiated user
object"(is that the correct terminology?).

I have been writing the user examples in the order that a real user
might usually take (A User who: is activating his account, is logging
in, forgets his password and resets it, wants to use a new email
address, wants to change his username.)

Now, I'm not sure of the next step in the process, I'm stuck! The method
(upgrade) takes a Payment object parameter. Inside the upgrade method,
some payment object attributes are set (user_id, payment_type) and then
saved. Then, the user object that is being upgraded has some attributes
that are set (payment_id, member, member_since) and finally the user is
saved, ending the method. And to top it all off, this method takes place
in a transaction.

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

Now that you have sufficient back story (I hope), here are my questions:

1.) Do I need to use any mocking/stubbing in this example (or in Unit
Tests)
2.) Is it wrong to access multiple objects (user and payment in my
example) in a Unit Test example?
3.) Would you mind showing me an example of how you might implement a
spec for this method.
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.

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.
Thanks for any help you can provide me on my journey - I have a long way
ahead of me.

Lake
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list