Module: Loggability::SpecHelpers

Defined in:
lib/loggability/spechelpers.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(context) ⇒ Object

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/loggability/spechelpers.rb', line 32

def self::included( context )
	context.around( :each ) do |example|
		if level = (example.[:log] || example.[: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

Instance Method Details

#reset_loggingObject

Reset the logging subsystem to its default state.



54
55
56
57
58
# File 'lib/loggability/spechelpers.rb', line 54

def reset_logging
	Loggability.formatter = nil
	Loggability.output_to( $stderr )
	Loggability.level = :fatal
end

#setup_logging(level = :fatal) ⇒ Object

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.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/loggability/spechelpers.rb', line 63

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