Skip to content
Advertisement

How to show SQL query log generated by a RSpec test?

I am writing a spec for my rails 3 application. I want to test that db transactions are really working. It would be really helpful to be able to see the sql queries being generated my app while being driven by the spec.

Is there a way to see the queries just like in the rails console?

I’m using Rails 3.0.9, RSpec 2.6, and sqlite (will move to mysql later on)

Advertisement

Answer

Put the following code in your specs:

Rails 7.0+

ActiveRecord.verbose_query_logs = true

Rails 5.2

ActiveRecord::Base.verbose_query_logs = true

Rails 3.0

ActiveRecord::Base.logger = Logger.new(STDOUT) if defined?(ActiveRecord::Base)

Thanks to everyone who commented for the Rails updates.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement