Why Not Use Shared Activerecord Connections For Rspec + Selenium

Why not use shared ActiveRecord connections for Rspec + Selenium?

This solution was written by Jose Valim - well respected in the Rails community and a member of the Rails core team. I doubt he would recommend using it if there were issues with it. I personally haven't had any issues.

Just be aware that if you use Spork this needs to be in the each_run block to work.

FWIW - I have had intermittent capybara test issues with the above patch on Postgres. The Mike Perham solution that @hsgubert has below appears to have solved those issues. I am now use that solution.

Rspec ignores establish_connection statement

Hope this helps someone — it turned out that I forced all ActiveRecord classes to use same shared connection following this answer: Why not use shared ActiveRecord connections for Rspec + Selenium?

Make sure to use separate shared connections for each database.

Selenium Chrome not seeing database changes with transactional fixtures enabled in Rails Rspec Docker environment

Rails 5.1 does support sharing the DB connection between the Application under test and the tests (transactional testing) but only if you're letting Capybara start the instance of the application under test, since the tests and the app need to be running as separate threads under the same process. You're specifically telling Capybara not to to run the app under test (Capybara.run_server = false) and instead telling it to run against an app instance you're starting separately (Capybara.app_host = "http://web:3000"). In that configuration there is no way to share the DB connection between the tests and the AUT so you have to disable transactional testing ('config.use_transactional_fixtures = false') and use database_cleaner (or something else similar) to handle resetting the DB between each test.

Using the rails gem database_cleaner when rSpec testing and using activerecord-sqlserver-adapter

Thanks for the heads up, but your stack overflow question is really not about SQL Server or the adapter. It is really a by product of how your software is put together. I'm gonna bet it all has something to do with how you establish connections too. Check out this comment on the adapter.

https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/173#issuecomment-4427121

Basically you want to make sure you only have one model that champions a connection so you can manage it. Most gems only know about ActiveRecord::Base. So if you have an app that has many databases, please make sure you do all the things that entails. It is tricky. So much so I even wrote a blog article about it. You should read it :)

http://technology.customink.com/blog/2015/06/22/rails-multi-database-best-practices-roundup/

Lastly... this is purely subjective... but I am a big fan of Rails transactional fixtures and not running your test with an empty database. My advice:

  • Use Rails defaults.
  • Layer a small amount of MiniTest on top. Use my minitest-spec-rails gem :)
  • Use factory girl... but leverage them in a way for dev/test synergies. See https://github.com/metaskills/named_seeds

Again, purely subjective, but I hope that last bit helps. Cheers.



Related Topics



Leave a reply



Submit