# Best way to code a bunch of unit tests?

**URL:** <https://www.ruby-forum.com/t/best-way-to-code-a-bunch-of-unit-tests/142653>\
**Category:** Rails\
**Created:** [July 10, 2008, 2:01am UTC](https://www.ruby-forum.com/t/best-way-to-code-a-bunch-of-unit-tests/142653 "2008-07-10T02:01:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 10, 2008, 2:01am UTC](https://www.ruby-forum.com/t/best-way-to-code-a-bunch-of-unit-tests/142653/1 "2008-07-10T02:01:22Z")

</div>

Hi All,

I’ve got a single unit test working on a two-variable Portfolio  
model. The model has transformations, pattern-matching etc. to clean  
up user sloppiness and ensure data quality.

To make sure the transformations/tests remain valid as the model  
undergoes future changes, I’ve got about a dozen tests codified in 3-  
element hashes: user-input, transformed-value-expected, error-  
messages (if any, expected to be produced by the validations)

What’s a better way to handling these triplets?

1. write a Ruby script to generate test\_1, test\_2, etc.
2. iterate through list, generate the test\_x’s and add those methods  
to the PortfoloTest class with metaprogramming.

(I know I could do it manually, but I don’t like to write repetitive  
code.)

I’ve got the simple working model and test code posted at  
[http://www.pastie.org/231118](http://www.pastie.org/231118). The test code included the hashes  
described above included as data in the unit-test file.

Thanks in advance for any suggestions you may offer.  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 10, 2008, 2:49am UTC](https://www.ruby-forum.com/t/best-way-to-code-a-bunch-of-unit-tests/142653/2 "2008-07-10T02:49:54Z")

</div>

I just started reading [Peak Obsession](http://manuals.rubyonrails.com/read/chapter/26)  
and earlier chapters. I see that fixtures are processed by eRB so  
that could be part of the story. Maybe its OK to generate the test  
fragments and stuff them all into a single test, perhaps  
test\_UserInput. This looks promising to me.

Regards,  
Richard

On Jul 9, 7:49 pm, RichardOnRails

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 10, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/best-way-to-code-a-bunch-of-unit-tests/142653/3 "2008-07-10T16:03:44Z")

</div>

I wrote up a long beginner’s guide to unit testing here:

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

You sound more advanced than beginner, but if you skim it, it might  
help answer some questions.

Best,  
Kevin

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 10, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/best-way-to-code-a-bunch-of-unit-tests/142653/4 "2008-07-10T18:43:38Z")

</div>

Thanks, Kevin. I’ll check it out.  
Regards.  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 13, 2008, 4:14am UTC](https://www.ruby-forum.com/t/best-way-to-code-a-bunch-of-unit-tests/142653/5 "2008-07-13T04:14:30Z")

</div>

Hey Kevin,

You could have been nice and pointed me directly to “Part 6:  
Fixtures”, instead of making me read all the preceding sections 🙂

Actually, the preceding sections presented several insights that I  
value. But learning that fixtures were preprocessed by eRB was the  
key to my problem. At least I think so. I’ll try to confirm that  
tomorrow.

Again, thank you very much for responding to my request for guidance.

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 13, 2008, 7:09am UTC](https://www.ruby-forum.com/t/best-way-to-code-a-bunch-of-unit-tests/142653/6 "2008-07-13T07:09:17Z")

</div>

I’ve been very happy writing unit tests with shoulda and factory\_girl.  
Shoulda makes tests (and output) easier to read, and it comes with  
some handy ones built in. Factory\_girl helps you do away with fixtures  
entirely!

Have a look:

> **[thoughtbot case studies featuring design and development projects for...](https://thoughtbot.com/case-studies)**
>
> Check out some thoughtbot case studies featuring design and development projects for websites, mobile and web applications across HealthTech, EdTech, FinTech B2B and B2C.

> **[thoughtbot case studies featuring design and development projects for...](https://thoughtbot.com/case-studies)**
>
> Check out some thoughtbot case studies featuring design and development projects for websites, mobile and web applications across HealthTech, EdTech, FinTech B2B and B2C.

On Jul 12, 10:13 pm, RichardOnRails

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 19, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/best-way-to-code-a-bunch-of-unit-tests/142653/7 "2008-07-19T22:40:31Z")

</div>

Hi G.S.,

Thanks for the links. I didn’t see them until today. The initial  
page of each looks great. I won’t dig into them further until I get  
my current unit test + fixtures working. I’m going to post my latest  
problem with them shortly.

Best wishes,  
Richard
