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?
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 .
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With