How do I turn on SQL debug logging for ActiveRecord in RSpec tests?

Ruby on-RailsRubyActiverecordRspec Rails

Ruby on-Rails Problem Overview


I have some RSpec tests for my models and I would like to turn on SQL ActiveRecord logging just like I see in the Rails server mode. How to do that?

I start my tests with

RAILS_ENV=test bundle exec rspec my/test_spec.rb

Thanks

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You could try setting the ActiveRecord logger to stdout in your test somewhere. If you're using rspec, maybe in the spec helper?

ActiveRecord::Base.logger = Logger.new(STDOUT)

Solution 2 - Ruby on-Rails

By default, all your db queries will be logged already in test mode. They'll be in log/test.log.

Solution 3 - Ruby on-Rails

set

config.log_level = :info 

in test environment

Solution 4 - Ruby on-Rails

if others answers don't work in your case, please check the 'log level' of your test environment.

its default is 'debug', which will output the SQL generated by Rails. if it was set to "info", the SQL will be missing.

Solution 5 - Ruby on-Rails

In your test.rb:

Rails.application.configure do
  ...
  config.logger = ActiveSupport::Logger.new(STDOUT)
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlzapView Question on Stackoverflow
Solution 1 - Ruby on-RailsGeorgeView Answer on Stackoverflow
Solution 2 - Ruby on-RailsidlefingersView Answer on Stackoverflow
Solution 3 - Ruby on-RailsNicolaiView Answer on Stackoverflow
Solution 4 - Ruby on-RailsSiweiView Answer on Stackoverflow
Solution 5 - Ruby on-RailsPere Joan MartorellView Answer on Stackoverflow