Loggability::

SpecHelpers module

Some helper functions for testing. Usage:

# in spec_helpers.rb
RSpec.configure do |c|
  c.include( Loggability::SpecHelpers )
end

# in my_class_spec.rb; set logging level to :error
describe MyClass, log: :error do

  # Except for this example, which logs at :debug
  it "does something", log: :debug do
    # anything the spec does here will be logged at :debug
  end

  it "does something else" do
    # but this will use the :error level from the 'describe'
  end

end

Public Class Methods

included( context )

Inclusion callback – install some hooks that set up logging for RSpec specs.

# File lib/loggability/spechelpers.rb, line 31
def self::included( context )
        context.around( :each ) do |example|
                if level = (example.metadata[:log] || example.metadata[:logging])
                        Loggability.with_level( level, &example )
                else
                        example.run
                end
        end

        context.before( :all ) do
                setup_logging()
        end

        context.after( :all ) do
                reset_logging()
        end

        super
end

Public Instance Methods

reset_logging()

Reset the logging subsystem to its default state.

# File lib/loggability/spechelpers.rb, line 53
def reset_logging
        Loggability.formatter = nil
        Loggability.output_to( $stderr )
        Loggability.level = :fatal
end
setup_logging( level=:fatal )

Alter the output of the default log formatter to be pretty in SpecMate output if HTML_LOGGING is set or TM_FILENAME is set to something containing _spec.rb.

# File lib/loggability/spechelpers.rb, line 62
def setup_logging( level=:fatal )

        # Only do this when executing from a spec in TextMate
        if ENV['HTML_LOGGING'] || (ENV['TM_FILENAME'] && ENV['TM_FILENAME'] =~ /_spec\.rb/)
                logarray = []
                Thread.current['logger-output'] = logarray
                Loggability.output_to( logarray )
                Loggability.format_as( :html )
                Loggability.level = :debug
        else
                Loggability.level = level
        end
end