I’ve been working on a gem in my spare time for code coverage and one of its goals is to output coverage reports to stdout. The hope is that while running something like guard for RSpec, also having test coverage output during the red-green-refactor cycling will be useful. What I didn’t realize beforehand is that output is fully testable within RSpec. Here’s an example of ensuring a proper message is sent via puts.

# lib/formatter.rb
class Formatter

  def print_coverage(covered_percent, lines_of_code)
    puts "#{covered_percent}% coverage, #{lines_of_code} total lines"


# spec/lib/formatter_spec.rb
it "displays the coverage" do
  expect {
    formatter.print_coverage(90, 127)
  }.to output(/90% coverage, 127 total lines/).to_stdout
"The output matcher provides a way to assert that the block has emitted content to either $stdout or $stderr."
- relishapp.com

This is accomplishing using RSpec’s output matcher. I could see this being useful for cli based gems the display feedback for developers.

« Previous Post
Configure Puma SSL for local development on Ubuntu
Next Post »
Formatting Enum columns into a human readable format with SQL

Join the conversation

comments powered by Disqus