如何在 rspec 测试中输出变量? [英] How do I output a variable in a rspec test?

查看:47
本文介绍了如何在 rspec 测试中输出变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种快速的方法可以在 rspec 测试中输出变量的值?像这样的东西,例如,在控制器中输出一个变量,我这样做:

Is there a quick way to output the value of variable in a rspec test? Something like this for example, in a controller to output a variable, I do:

raise variable.to_yaml

我可以在 rspec 测试中做类似的事情来查看变量的内容吗?

Is there something similar I can do in a rspec test to see the contents of a variable?

推荐答案

如果您希望输出进入日志文件(即 logs/test.log),您可以使用 rails 记录器.

If you want the output to go into the log file (i.e. logs/test.log), you can use the rails logger.

Rails.logger.debug variable.inspect
Rails.logger.debug variable.to_yaml

如果你想在控制台看到输出,你可以使用漂亮的打印机'pp'.

If you want to see the output in the console, you can use the pretty printer 'pp'.

require 'pp'

it 'does something'
  thing = Factory(:something)
  pp thing
end

或者你可以使用好的 'ol puts

Or you can use good 'ol puts

puts thing.to_yaml

这篇关于如何在 rspec 测试中输出变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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