Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Testing: Fixtures, Factories, and Magic numbers

I've got an application that needs quite a bit of data (1000s of records) to do appropriate testing. The only way I've found to get a decent set of testable, sensible data is to use a subset of my production DB. I've converted this to YAML fixtures in the normal `test/fixtures' location.

This works, but now I have a bunch of seemingly brittle tests and assertions that depend on their being a particular number of records that meet condition X...

example

def test_children_association
  p = Parent.find(1)
  assert_equal 18, p.children.count, "Parent.children isn't providing the right records"
end

This doesn't seem like a good idea to me, but I'm not sure if there is a better / accepted way to test an application that needs a large hierarchy of data.

like image 620
Daniel Beardsley Avatar asked Oct 22 '08 23:10

Daniel Beardsley


1 Answers

Magic numbers in tests aren't an anti-pattern. Your tests need to be so dead-simple that you don't need to test them. This means you'll have some magic numbers. This means that your tests will break when you change small bits of functionality. This is good.

Fixtures have some problems, but there are a few simple things you can do to make them easier to work with:

  1. Only have baseline data in your fixtures, the sort of data that most of your tests need but don't care about. This will involve a time investment up front, but it's better to take the pain early than write poor unit tests for the life of the project.

  2. Add the data to be tested in the context of the test. This improves readability of your tests and saves you from writing "make sure nobody messed up the fixtures" sanity checks at the beginning of your unit tests.

like image 149
marcumka Avatar answered Nov 07 '22 06:11

marcumka