testing local vars and exceptions

Discussion in 'Ruby' started by Raymond O'connor, Jan 31, 2007.

  1. Hi
    Two questions:
    I'm new to testing, so my apologies if this is really basic, but is
    there anyway to test the value of a local variable in my unit tests? So
    far I've been making everything instance vars that don't really need to
    be, so I can see their value in my unit tests.

    Also, one of my methods calls another method in a 3rd party library that
    could throw an exception if there's no network connection. My method
    has some special logic in it to handle this exception. I would like to
    test this in a unit test for the method but I can't figure out how to
    throw this exception since its raised in a method I don't have control
    over. I guess I could put a 'raise exception if test' in my method but
    that doesn't seem right. Is there any better way to test this?

    Thanks in advance for the help,
    -Ray

    --
    Posted via http://www.ruby-forum.com/.
     
    Raymond O'connor, Jan 31, 2007
    #1
    1. Advertising

  2. Raymond O'connor

    Pit Capitain Guest

    Raymond O'connor schrieb:
    > I'm new to testing, so my apologies if this is really basic, but is
    > there anyway to test the value of a local variable in my unit tests? So
    > far I've been making everything instance vars that don't really need to
    > be, so I can see their value in my unit tests.


    I have never needed to test local variables. Can you give an example why
    you would like to do this?

    > Also, one of my methods calls another method in a 3rd party library that
    > could throw an exception if there's no network connection. My method
    > has some special logic in it to handle this exception. I would like to
    > test this in a unit test for the method but I can't figure out how to
    > throw this exception since its raised in a method I don't have control
    > over. I guess I could put a 'raise exception if test' in my method but
    > that doesn't seem right. Is there any better way to test this?


    Test doubles. See
    http://www.martinfowler.com/articles/mocksArentStubs.html for an
    explanation. There are some Ruby frameworks you can use. Look for "mock"
    on http://rubyforge.org.

    Regards,
    Pit
     
    Pit Capitain, Jan 31, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    410
  2. Linuxguy123
    Replies:
    7
    Views:
    713
    Paddy O'Loughlin
    Feb 20, 2009
  3. caccolangrifata
    Replies:
    18
    Views:
    413
    Chris Torek
    Jul 22, 2011
  4. Dirk Einecke

    GET-parameters an local vars

    Dirk Einecke, May 15, 2004, in forum: Ruby
    Replies:
    4
    Views:
    110
    Dirk Einecke
    May 15, 2004
  5. Greg Willits

    Unit Testing and Instance Vars

    Greg Willits, Jul 25, 2009, in forum: Ruby
    Replies:
    4
    Views:
    123
    Greg Willits
    Jul 25, 2009
Loading...

Share This Page