views:

256

answers:

2

I am using rspec for my test in a ruby project, and I want to spec that my program should not output anything when the -q option is used. I tried:

Kernel.should_not_receive :puts

That did not result in a failed test when there was output to the console.

How do I verify the absents of text output?

+5  A: 

puts uses $stdout internally. Due to the way it works, the easiest way to check is to simply use: $stdout.should_not_receive(:write)

Which checks nothing is written to stdout as expected. Kernel.puts (as above) would only result in a failed test when it is explictely called as such (e.g. Kernel.puts "Some text"), where as most cases it's call in the scope of the current object.

Sutto