Skip to content

Instantly share code, notes, and snippets.

@adamgoucher
Created February 24, 2011 04:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamgoucher/841771 to your computer and use it in GitHub Desktop.
Save adamgoucher/841771 to your computer and use it in GitHub Desktop.
how to hack in log messages to rspec 2
When doing automation I coach people not to put in logging in their scripts aside from things like 'here is the user name and password I randomly created just now' but I couldn't coax the default documentation formatter in rspec to capture information send via 'puts'. So I over-engineered a solution. Proper documentation and inclusion in my rspec-selenium-pageobjects project in a day or so.
spec_helper.rb
--------------
module RSpec
module Core
class Reporter
def with(message)
notify :with, message
end
def notify(method, *args, &block)
@formatters.each do |formatter|
if formatter.respond_to?(method)
formatter.send method, *args, &block
end
end
end
end
end
end
def with(message)
RSpec.configuration.reporter.with message
end
you_spec.rb
-----------
it "goes to the story", :homepage => true do
clicked = @home.goto_random_section_story
with 'this rabbit hole'
with 'is hella deep'
end
logging_documentation_formatter.rb
----------------------------------
require 'rspec/core/formatters/documentation_formatter'
class LoggingDocumentationFormatter < RSpec::Core::Formatters::DocumentationFormatter
def initialize(output)
super(output)
@group_level = 0
@with_messages = Array.new
end
def with(message)
@with_messages << (grey("#{current_indentation}with: #{message}"))
end
def flush
@with_messages.length.times{@with_messages.shift}
end
def example_passed(example)
super(example)
output.puts @with_messages.join("\n")
flush
end
def example_failed(example)
super(example)
output.puts @with_messages.join("\n")
flush
end
end
how to use
----------
rspec --require spec/support/formatters/logging_documentation_formatter.rb -f LoggingDocumentationFormatter -c spec/your_spec.rb
output
------
goes to the story
with: this rabbit hole
with: is hella deep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment