Search code examples
ruby-on-rails-3postgresqlactiverecordrspec2

Manually reconnecting to the database in Rails tests


Ok so I'm trying to do something a bit sneaky to speed up my test suite. I have a Event Machine based server connected to the database which my rails app talks to.

Spinning up this EM Server on each test takes forever so I was hoping to leave it running throughout the execution of the entire test suite.

However, the EM Server process talks to the DB and gets very upset when the Test DB is reset at the end of each test throwing lots of nasty looking Postgres errors.

I think I would be able to circumvent this problem if I could tell the EM server to reset it's connection to the Test DB at the start of each new test.

Any ideas on how I can do this?


Solution

  • Have you tried calling reconnect! on the adapter?

    http://apidock.com/rails/ActiveRecord/ConnectionAdapters/AbstractAdapter/reconnect%21