views:

450

answers:

5

I find fixtures a bit tedious to use in Ruby on Rails, partly because I need to manually correct them after most migrations.

Is there a way (a plugin?) I can have my fixtures modified when I run my migrations?

A: 

I'm fairly sure there's a plugin that dumps the current contents of a database out to fixture files (although I don't remember it's name off the top of my head). One way to handle this problem would be to load the fixtures, run the migrations, then use this plugin to dump the fixtures back out.

On the other hand, if your DB schema has changed, the chances are that your test conditions, corner cases, and constraints are now different, so a manual review of your fixtures and test cases probably isn't a bad idea.

womble
A: 

I too find fixtures a bit tedious, which is why I try not to use them.

Instead, I use mocks. Check out rspec or mocha.

pope
+1  A: 

There are a lot of debates in the Rails community about using or not using fixtures. Personally I'm using factory_girl which I manually update when I make changes to models.

If you choose to use fixtures, in my opinion you should manually update them because they are a part of your test suite, it must check that your code works as intended.

Mihai A
+2  A: 

Adding to womble's answer above, you can generate fixtures from your dev database using topfunky's ar_fixures plugin:

http://github.com/topfunky/ar_fixtures/tree/master

In my opinion, there's still a valid place for a small, well managed set of fixtures in rails - as there is for mock objects... but we'll leave that for another day!

Also worth considering the fixture-sets-for-rails plugin, though I haven't used it myself: http://thatswhatimtalkingabout.org/news/2006/8/31/fixture-sets-for-rails Seems like a reasonable idea, although I'd be very wary of building a mass of confusing sets of fixtures - each of which you may have to fix when your domain model changes. I'd probably stick to mocks instead (this plugin looks quite old and was probably written before a mocha turned up).

Martin Dow
A: 

Factory Girl or Machinist are both great alternatives to fixtures, you just need to define a valid instance of your model and then use as many or as few as you need. I was using Machinist, but started using Factory Girl as it has better support for associations between your models.

railsninja