Ruby-on-rails 如何在 RSpec 测试中为 ActiveRecord 打开 SQL 调试日志记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5244486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I turn on SQL debug logging for ActiveRecord in RSpec tests?
提问by lzap
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?
我对我的模型进行了一些 RSpec 测试,我想打开 SQL ActiveRecord 日志记录,就像我在 Rails 服务器模式中看到的那样。怎么做?
I start my tests with
我开始我的测试
RAILS_ENV=test bundle exec rspec my/test_spec.rb
Thanks
谢谢
采纳答案by idlefingers
By default, all your db queries will be logged already in test mode. They'll be in log/test.log.
默认情况下,您的所有数据库查询都将在测试模式下记录。他们会在log/test.log。
回答by George
You could try setting the ActiveRecord logger to stdout in your test somewhere. If you're using rspec, maybe in the spec helper?
您可以尝试在测试中的某个地方将 ActiveRecord 记录器设置为 stdout。如果您使用的是 rspec,也许在规范助手中?
ActiveRecord::Base.logger = Logger.new(STDOUT)
回答by Nicolai
set
放
config.log_level = :info
in test environment
在测试环境
回答by u445908
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.
它的默认值是 'debug',它将输出由 Rails 生成的 SQL。如果设置为“info”,则 SQL 将丢失。
回答by Pere Joan Martorell
In your test.rb:
在您的test.rb:
Rails.application.configure do
...
config.logger = ActiveSupport::Logger.new(STDOUT)
end

