check for (the absence of) `puts` in RSpec

bdd, mocking, rspec, ruby, tdd

Solution

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.

Problem

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?

Original source