117

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

Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
lzap
  • 16,417
  • 12
  • 71
  • 108

5 Answers5

402

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)
George
  • 4,273
  • 2
  • 15
  • 10
63

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

philippe_b
  • 38,730
  • 7
  • 57
  • 59
idlefingers
  • 31,659
  • 5
  • 82
  • 68
11

set

config.log_level = :info 

in test environment

Nicolai
  • 321
  • 3
  • 7
  • 4
    or set it to config.log_level = :debug to get maximum output, including each SQL statement executed – Zack Xu Jul 12 '16 at 10:40
  • What config thing is this? As one might expect, I get "undefined local variable or method 'config' for main:Object". *Edit:* ah this is a Rails thing and not an ActiveRecord thing, nvm... – Luc Apr 10 '21 at 22:01
8

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.

Siwei
  • 19,858
  • 7
  • 75
  • 95
2

In your test.rb:

Rails.application.configure do
  ...
  config.logger = ActiveSupport::Logger.new(STDOUT)
end
Pere Joan Martorell
  • 2,608
  • 30
  • 29