# Testing local vars and exceptions

**URL:** <https://www.ruby-forum.com/t/testing-local-vars-and-exceptions/83980>\
**Category:** Ruby\
**Created:** [January 31, 2007, 5:00am UTC](https://www.ruby-forum.com/t/testing-local-vars-and-exceptions/83980 "2007-01-31T05:00:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [January 31, 2007, 5:00am UTC](https://www.ruby-forum.com/t/testing-local-vars-and-exceptions/83980/1 "2007-01-31T05:00:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [January 31, 2007, 12:35pm UTC](https://www.ruby-forum.com/t/testing-local-vars-and-exceptions/83980/2 "2007-01-31T12:35:04Z")

</div>

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  
[Mocks Aren't Stubs](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](http://rubyforge.org).

Regards,  
Pit
