double free
double free

Reputation: 1067

How do I output a variable in a rspec test?

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

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

Upvotes: 44

Views: 63927

Answers (4)

AYOUB_MZ
AYOUB_MZ

Reputation: 11

You can use the gem pry to do this

  1. add gem "pry" in your gemfile
  2. bundle install

Now you can any test any variable by putting "binding.pry" just after that variable. Run bundle exec rspec filepath and you will get something like rails c, then write directly your variable.

I hope it makes sense

Upvotes: 1

knagode
knagode

Reputation: 6125

Use this:

$stderr.puts variable.to_yaml

Upvotes: 4

Puce
Puce

Reputation: 1021

At this moment (Rails 4) you can log it:

p variable

Upvotes: 7

ipd
ipd

Reputation: 5714

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

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

Or you can use good 'ol puts

puts thing.to_yaml

Upvotes: 77

Related Questions