# Help in writing unit test cases

**URL:** <https://www.ruby-forum.com/t/help-in-writing-unit-test-cases/154215>\
**Category:** Rails\
**Created:** [November 26, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/help-in-writing-unit-test-cases/154215 "2008-11-26T14:02:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@sumanth](https://www.ruby-forum.com/u/sumanth)\
**Post date:** [November 26, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/help-in-writing-unit-test-cases/154215/1 "2008-11-26T14:02:02Z")

</div>

Hi,

I’ve been working on ROR just for 4 months. I am naive in writing test  
cases. I heard that one has to start with unit tests. So, I am  
confused to where and how to start with. Any materials available or  
any guides which helps me with some examples also.

Thanks in advance  
sumanth

---

<div class="post-metadata">

**Author:** ![sumanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@sumanth](https://www.ruby-forum.com/u/sumanth)\
**Post date:** [November 26, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/help-in-writing-unit-test-cases/154215/2 "2008-11-26T14:12:43Z")

</div>

Hello,

If you are using scaffolding to create your models and controllers  
then you have the test files for each model and controller already  
created within “test” directory.

Thie rails wiki is always a good starting point:

[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest](http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest)

Hank

---

<div class="post-metadata">

**Author:** ![sumanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@sumanth](https://www.ruby-forum.com/u/sumanth)\
**Post date:** [November 26, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/help-in-writing-unit-test-cases/154215/3 "2008-11-26T14:19:46Z")

</div>

Hi blasterpal,  
First I should thank you for the quick response.

I am not using scaffolding to create controllers and models. So i dont  
have  
test files for them. But I used scaffolding just to try it. I am aware  
of  
how those test files are like.

But if I have to write a unit test case for a existing controller, how  
should I start with. Should I proceed method by method. Can we be  
specific  
for a method to write a test case. Like, can we just analyze a function  
in  
the model, testing it for its inputs and outputs etc.

-sumanth.

---

<div class="post-metadata">

**Author:** ![sumanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@sumanth](https://www.ruby-forum.com/u/sumanth)\
**Post date:** [November 26, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/help-in-writing-unit-test-cases/154215/4 "2008-11-26T22:27:17Z")

</div>

sumanth wrote:

> ```
> created within "test" directory.
> >
> > I've been working on ROR just for 4 months. I am naive in
> writing test
> > cases. I heard that one has to start with unit tests. So, I am
> > confused to where and how to start with. Any materials
> available or
> > any guides which helps me with some examples also.
> >
> > Thanks in advance
> > sumanth
> 
> ```

My understanding is that the unit tests are really for testing of the  
model and any methods in in. I understand that the controllers are  
tested using the functional tests and combinations with the integration  
tests. I have only gotten as far as the unit tests so far and I have  
only written unit tests for cases where I have added methods to the  
models.

My understanding could be wrong but it is what I understand.

Norm

---

<div class="post-metadata">

**Author:** ![sumanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@sumanth](https://www.ruby-forum.com/u/sumanth)\
**Post date:** [November 26, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/help-in-writing-unit-test-cases/154215/5 "2008-11-26T15:11:31Z")

</div>

Provided, there exists a method which gets the data from the table of  
one  
model. It takes some variables. While fetching the data, it interacts  
with  
other tables also. We have 2 to 3 different queries which are  
interlinked.  
Meaning by result from one query are used in the next. How one should  
start  
writing a test for it?  
What aspects should be tested?  
I am confused, dunno where to start?

Any help is much appreciated.

Thanks  
sumanth

---

<div class="post-metadata">

**Author:** ![sumanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@sumanth](https://www.ruby-forum.com/u/sumanth)\
**Post date:** [November 27, 2008, 6:22pm UTC](https://www.ruby-forum.com/t/help-in-writing-unit-test-cases/154215/6 "2008-11-27T18:22:39Z")

</div>

Sumanth,

I wrote a series of blog posts that might help demystify unit testing  
for you.

It starts here:  
[http://www.nullislove.com/2007/11/10/testing-in-rails-introduction/](http://www.nullislove.com/2007/11/10/testing-in-rails-introduction/)

HTH,  
Kevin
