How to show SQL query log generated by a RSpec tes

2020-05-18 06:50发布

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)

2条回答
萌系小妹纸
2楼-- · 2020-05-18 07:49

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.

查看更多
Evening l夕情丶
3楼-- · 2020-05-18 07:50

According to the Rails 5 docs (Active Record Explain). You can now use the explain method. This will only log the queries you specify, but you will not be overwhelmed if you have a large amount of SQL code before your tests.

ap Model.explain
查看更多
登录 后发表回答