Where to put code, tests, and test data


#1

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


#2

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