lzap
lzap

Reputation: 17174

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

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

Upvotes: 125

Views: 100894

Answers (5)

Pere Joan Martorell
Pere Joan Martorell

Reputation: 3103

In your test.rb:

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

Upvotes: 4

idlefingers
idlefingers

Reputation: 32037

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

Upvotes: 69

Nicolai
Nicolai

Reputation: 341

set

config.log_level = :info 

in test environment

Upvotes: 13

Siwei
Siwei

Reputation: 21549

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.

Upvotes: 10

George
George

Reputation: 4473

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)

Upvotes: 421

Related Questions