Best way to code a bunch of unit tests?

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 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

I just started reading Peak Obsession 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

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

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

Best, Kevin

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

Hey Kevin,

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

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

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:

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