如何在 rspec 测试中输出变量?

如何在 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