Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing response in rspec

I have a test I can't get to work, so I want to debug my spec by printing the reponse.

I've tried using print response.body in my spec, but there's no output in either test.log or the console.

I'm probably missing something. What should I try?

Thanks!

like image 873
Simon Fredsted Avatar asked Apr 19 '12 10:04

Simon Fredsted


People also ask

How do I mock a method in RSpec?

Mocking with RSpec is done with the rspec-mocks gem. If you have rspec as a dependency in your Gemfile , you already have rspec-mocks available.

What is double in RSpec?

RSpec features doubles that can be used as 'stand-ins' to mock an object that's being used by another object. Doubles are useful when testing the behaviour and interaction between objects when we don't want to call the real objects - something that can take time and often has dependencies we're not concerned with.


2 Answers

You can print the response using:

raise response.body 
like image 63
Lee Irving Avatar answered Nov 05 '22 23:11

Lee Irving


You can use

$stderr.puts response.body

To print your Response. But I will suggest to just use above code for testing and remove any puts from RSpec Unit test it is considered bad practice to write RSpec code that output something.

like image 44
Hitesh Ranaut Avatar answered Nov 06 '22 00:11

Hitesh Ranaut