# Where to put code, tests, and test data

**URL:** <https://www.ruby-forum.com/t/where-to-put-code-tests-and-test-data/163776>\
**Category:** Rails\
**Created:** [March 24, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/where-to-put-code-tests-and-test-data/163776 "2009-03-24T19:45:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [March 24, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/where-to-put-code-tests-and-test-data/163776/1 "2009-03-24T19:45:38Z")

</div>

I am working on an RSS reader in Rails. I.e., Feed has\_many Article.  
Reading  
the feeds is done from a cron job. Currently the feed reading code is  
in the  
lib directory. I’m not sure how to test code in lib. And I’m not sure  
that  
is the best place for it. Can someone make a good case for the Feed  
model?  
It is mostly about pulling data out of an XML file with no real access  
needed  
to the internals of the Feed and Article models beyond the usual model  
API.

The test data is XML files. Should this go in test/fixtures? Or  
somewhere  
else.

Thoughts?

TIA,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [March 25, 2009, 2:07am UTC](https://www.ruby-forum.com/t/where-to-put-code-tests-and-test-data/163776/2 "2009-03-25T02:07:20Z")

</div>

Jeffrey L. Taylor wrote:

> I am working on an RSS reader in Rails. I.e., Feed has\_many Article. Reading  
> the feeds is done from a cron job. Currently the feed reading code is in the  
> lib directory. I’m not sure how to test code in lib. And I’m not sure that  
> is the best place for it. Can someone make a good case for the Feed model?  
> It is mostly about pulling data out of an XML file with no real access needed  
> to the internals of the Feed and Article models beyond the usual model API.
> 
> The test data is XML files. Should this go in test/fixtures? Or somewhere  
> else.

Roughly speaking, if you put things in nearly the right place, Rails  
will find it.

Given lib/foo.rb, the first consideration is whether its Foo class is  
really a  
model. If it coheres to your other models, and is not just a utility,  
then maybe  
it should move to app/models.

Next, just write test/unit/foo\_test.rb. Besides, you should have already  
written  
it - Test-Driven Development works much, much better than test-last!

If you put your sample XML files into test/fixtures/foo\_files/, then you  
can  
pull them in with some test-side method like…

def assemble\_foo\_xml(filename)  
xml = File.read(RAILS\_ROOT + ‘test/fixtures/foo\_files/’ + filename

- ‘.xml’)  
SomeXmlLib.Reader.new(xml)  
end

Call that at the top of each test case, and name each filename after the  
themes  
found in the various tests.

–  
Phlip
