如何显示 RSpec 测试生成的 SQL 查询日志? [英] How to show SQL query log generated by a RSpec test?

查看:15
本文介绍了如何显示 RSpec 测试生成的 SQL 查询日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的 rails 3 应用程序编写规范.我想测试数据库事务是否真的有效.能够看到在规范驱动下生成我的应用程序的 sql 查询真的很有帮助.

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.

有没有办法像在 rails 控制台中一样查看查询?

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

我正在使用 Rails 3.0.9、RSpec 2.6 和 sqlite(稍后将移至 mysql)

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

推荐答案

把它放在你的规范中:

ActiveRecord::Base.logger = Logger.new(STDOUT) 如果定义了?(ActiveRecord::Base)

你可以tail -f log/test.log 看看发生了什么.一个但不是两个.

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

针对 Rails 5.2 进行编辑,添加以下行:

Edit for Rails 5.2, add this line:

ActiveRecord::Base.verbose_query_logs = true

感谢 Mike Monteiro 的更新!

这篇关于如何显示 RSpec 测试生成的 SQL 查询日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆