Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 785
gdelfino Avatar asked Jul 30 '11 16:07

gdelfino


People also ask

How do I display a SQL query?

The DISPLAY command must be placed immediately after the query statement on which you want it to take effect. For example: SELECT pno, pname FROM part WHERE color='BLUE'; DISPLAY; When the system encounters this DISPLAY command, it displays the Result window containing the part number and name for all blue parts.


1 Answers

Put this in your specs:

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

and you can tail -f log/test.log and see what's happening. Either one but not both.

Edit for Rails 5.2, add this line:

ActiveRecord::Base.verbose_query_logs = true 

Thanks to Mike Monteiro for the update!

like image 87
Chris Ledet Avatar answered Sep 17 '22 22:09

Chris Ledet