Posted over 5 years ago. Visible to the public.

Testing terminal output with RSpec

When testing Ruby code that prints something to the terminal, you can test that output.
Since RSpec 3.0 there is a very convenient way to do that.

Anything that writes to stdout (like puts or print) can be captured like this:

Copy
expect { something }.to output("hello\n").to_stdout

Testing stderr works in a similar fashion:

Copy
expect { something }.to output("something went wrogn\n").to_stderr

Hint: Use heredoc to test multi-line output.

Copy
expect { something }.to output(<<-MESSAGE.strip_heredoc).to_stdout Dear Bob, greetings! Love, Alice MESSAGE

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
over 5 years ago
by Arne Hartherz
Keywords:
console
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more