0

I've got a ruby on rails application with significant database seeds that tie into the UI elements and are thusly required for integration tests.

I'm using database_cleaner to clean up after my tests, but due to the heavy js use of the application most of my tests can't be run with the transaction strategy which allows me to specify tables to leave out of the rollback.

The result is that I've got to re-seed the database before each test or deal with a dirty database. Does anyone have tips for testing in this kind of situation or know of any tools that may help?

Thanks!

biagidp
  • 2,175
  • 3
  • 18
  • 29

1 Answers1

0

I've dealt with this in the past by using transactions for our tests (we're using something a little homegrown for that at the moment but previously we used database cleaner).

The key is forcing everything to use the same database connection. The path I've been down for this is to set the database pool size in test to 1: everyone has to share the same connection because active record won't let you create more connections.

For this to work you need to be careful about releasing connections no longer in use, by calling ActiveRecord::Base.clear_active_connections!

There are 2 places you need to do this

  • when your test code is about to call visit, click etc.
  • after each request (use a Rack middleware for this- you can basically copy ActiveRecord::ConnectionAdapters::ConnectionManagement)

This works for us writing specs against pages that use ajax to fetch intermediate data and so on, but can be a bit tricky to get working initially. I wrote about it more extensively a while back

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174