Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 555
double free Avatar asked May 17 '11 03:05

double free


People also ask

What is let in RSpec?

let generates a method whose return value is memoized after the first call. This is known as lazy loading because the value is not loaded into memory until the method is called. Here is an example of how let is used within an RSpec test. let will generate a method called thing which returns a new instance of Thing .

How do I run a specific test in RSpec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

How do I run a RSpec test in terminal?

Open your terminal, cd into the project directory, and run rspec spec . The spec is the folder in which rspec will find the tests. You should see output saying something about “uninitialized constant Object::Book”; this just means there's no Book class.

What RSpec method is used to create an example?

The it Keyword. The word it is another RSpec keyword which is used to define an “Example”. An example is basically a test or a test case. Again, like describe and context, it accepts both class name and string arguments and should be used with a block argument, designated with do/end.


1 Answers

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 
like image 90
ipd Avatar answered Sep 22 '22 17:09

ipd