简体   繁体   English

如何仅在Rails的测试环境中插入日志?

[英]How can I insert logs just in test environment in Rails?

I want to put some code ONLY in test environment, for example insert a line of log: 我只想在测试环境中放置一些代码,例如插入一行日志:

puts "Something in TEST"

What's the best way to do it? 最好的方法是什么?

Before Rails 2.x, the best way to get the current environment was using RAILS_ENV . 在Rails 2.x之前,获取当前环境的最佳方法是使用RAILS_ENV Rails 2.x or later, Rails introduced the Rails module with method Rails.env . 在Rails 2.x或更高版本中,Rails通过Rails.env方法引入了Rails模块。 So you can use 所以你可以使用

Before 2.x: if RAILS_ENV == 'test' 在2.x之前: if RAILS_ENV == 'test'

After 2.x: if Rails.env.test? 在2.x之后: if Rails.env.test?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定我的 rails 是否在开发环境中而不是在测试环境中? - How can I determine if my rails is in the development environment and not the test environment? 如何在rails开发环境中测试错误页面? - How can I test error pages in rails development environment? 如何在Rails的测试环境中添加Rack Middleware? - How can I add Rack Middleware to the test environment in Rails? 如何测试生产环境而不是测试环境Rails - How can I test my production environment instead of my test environment Rails 如何防止在Rails测试环境中对CSS文件进行预编译 - How can I stop css files from being precompiled in rails test environment 如何使 rails 中的自定义环境成为默认环境? - How can I make a custom environment in rails a default environment? 我该如何检查coffeescript中的Rails测试环境? - How do I check for the test environment in coffeescript for rails? Rails:如何在没有表单的情况下插入text_field来测试布局? - Rails: How to insert a text_field without a form just to test the layout? 如何在 Rails 日志中包含响应数据? - How can I include response data in Rails logs? 如何在Rails 4中禁用所有CLI测试日志? - How to disable all CLI test logs in Rails 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM