views:

325

answers:

2

I'm currently looking at a hefty Rails test suite. It's nothing I can get into specifics about, but the run time for the entire suite (unit/functional/some integration) can run upward of 5 minutes.

We're completely reliant on fixtures and are we're not mocking and stubbing as much as we should be.

Our next few sprints are going to be completely focused on the test suite, both improving coverage, writing better tests and most importantly writing more efficient tests.

So aside from more mocking and stubbing within our tests, we're considering replacing our fixtures with most likely Factory Girl. I see a lot of happy folks doing similar situations but haven't been able to find a good resource on any minuses of moving to a factory. I have seen some slower benchmarks when using benchmarks from various resources but cannot find a definitive this why factories are good and this is why you might not want to use them.

Can anyone educate me on why or why I shouldn't be using factories?

Thanks!

+5  A: 

There could be some issues with setting up all dependencies between entities for good test suite. Anyway, it's still much easier than maintaing a lot of fixtures.

Fixtures:

  • hard to maintain relationships (especially many-to-many);
  • test suite runtime is usually slower due more DB hits;
  • tests are very sensitive to changes in schema.

Factories:

  • you stub everything you don't test at current unit test;
  • you prepare entities that you are testing with factories. This is where factories show their real advantage — it's easy to set up new test cases, as you don't need to maintain a ton of YAML-files for that;
  • you concentrate on testing. If tests require changing scenario, you don't shift your mindset. As long as stubs are reasonable and factories are easily customized, you should be fine.

So, factories seem a good way to go. The only possible drawbacks I see, are:

  • time you spent migrating from fixtures;
  • keeping a sane set of scenarios can require some effort.
Oleg Shaldybin
+5  A: 
dasil003