Hide output in rspec tests, the easy way

Sometimes you have code, maybe a script of some kind, that you need to test, but the script contains puts statements that will pollute your test output. An easy fix in rspec to hide all of this output is to add this to your spec file that tests the script:

before do
  STDOUT.stubs(:write)
end

I found this method out by puts debugging a new test I was writing, but couldn’t see any output when running the tests.

A better method though would be to use StringIO for your puts statements which I wrote about in Preventing console output in tests.