# Unit testing

**URL:** <https://www.ruby-forum.com/t/unit-testing/154292>\
**Category:** Rails\
**Created:** [November 27, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/unit-testing/154292 "2008-11-27T12:40:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumanth\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sumanth\_tt](https://www.ruby-forum.com/u/sumanth_tt)\
**Post date:** [November 27, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/unit-testing/154292/1 "2008-11-27T12:40:18Z")

</div>

Hello,

Can somebody help me writing a unit test for existing method of a model.

I am using 2 tables like users, articles and

Method is like

def get\_the\_user\_bought\_item(user\_id\_list)  
users\_list = User.find\_by\_sql([“SELECT \* FROM users WHERE id IN (?)”,  
user\_id\_list])  
item\_details=Article.find\_by\_sql([“SELECT \* FROM users WHERE  
bought\_by IN  
(?)”, user\_id\_list])  
return user\_list, item\_details  
end

So i need to write a unit test for this. How that unit test should look  
like?

Thank you  
sumanth

---

<div class="post-metadata">

**Author:** ![sumanth\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sumanth\_tt](https://www.ruby-forum.com/u/sumanth_tt)\
**Post date:** [November 27, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/unit-testing/154292/2 "2008-11-27T12:52:01Z")

</div>

On Nov 27, 9:04 am, “sumanth tt” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> user\_id\_list])  
> item\_details=Article.find\_by\_sql([“SELECT \* FROM users WHERE bought\_by IN  
> (?)”, user\_id\_list])  
> return user\_list, item\_details  
> end
> 
> So i need to write a unit test for this. How that unit test should look  
> like?

pretty simple really. Setup some preconditions (fixtures, model  
instances created as needed, mocks etc…), call the method and assert  
that the results are as expected. Try the common case and some edge  
cases (eg user\_item\_list is empty etc…).

Your current implementation is rather low level. That could be

users\_list = User.find user\_id\_list  
items = Article.find\_all\_by\_bought\_by user\_id\_list  
return users\_list, items

Fred

---

<div class="post-metadata">

**Author:** ![sumanth\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sumanth\_tt](https://www.ruby-forum.com/u/sumanth_tt)\
**Post date:** [November 27, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/unit-testing/154292/3 "2008-11-27T16:01:06Z")

</div>

Hey Frederick,

Thank you very much. I just implemented the same way you have mentioned.  
As  
I added a test by test, I understood it well and could think of other  
possible cases also.

If we have to check for the internal portions, we do it by inspecting  
(raising the variable ) while developing. If we have to do the same job  
by  
tests, how can we do that?

As you mentioned i changed my way of querying as you said. Special  
thanks  
for that. 🙂

Thanks  
Sumanth

On Thu, Nov 27, 2008 at 5:21 PM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![sumanth\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sumanth\_tt](https://www.ruby-forum.com/u/sumanth_tt)\
**Post date:** [November 28, 2008, 2:47am UTC](https://www.ruby-forum.com/t/unit-testing/154292/4 "2008-11-28T02:47:36Z")

</div>

sumanth tt wrote:

> If we have to check for the internal portions, we do it by inspecting  
> (raising the variable ) while developing. If we have to do the same job  
> by tests, how can we do that?

By designing your code for testing, and writing tests on all the little  
bitty  
intermediate methods.

Without testing, these methods might be private - or worse, they might  
just be  
statements embedded in run-on procedures! But testability is more  
important than  
privacy, so more things will be public.

–  
Phlip  
[Radar – O’Reilly](http://broadcast.oreilly.com/2008/10/testing-rails-partials.html)
