class RSpec::Core::Notifications::FailedExampleNotification
@see ExampleNotification
@attr [RSpec::Core::Example] example the current example
end
puts notification.exception.backtrace.join(“n”)
puts “Here’s my stack trace”
puts “Hey I failed :(”
def example_failed(notification)
@example
things useful for failed specs.
The ‘FailedExampleNotification` extends `ExampleNotification` with
def backtrace_formatter
def backtrace_formatter RSpec.configuration.backtrace_formatter end
def colorized_formatted_backtrace(colorizer = ::RSpec::Core::Formatters::ConsoleCodes)
-
(Array(String))
- the examples colorized backtrace lines
Parameters:
-
colorizer
(#wrap
) -- An object to colorize the message_lines by
def colorized_formatted_backtrace(colorizer = ::RSpec::Core::Formatters::ConsoleCodes) formatted_backtrace.map do |backtrace_info| colorizer.wrap "# #{backtrace_info}", RSpec.configuration.detail_color end end
def colorized_message_lines(colorizer = ::RSpec::Core::Formatters::ConsoleCodes)
-
(Array(String))
- The example failure message colorized
Parameters:
-
colorizer
(#wrap
) -- An object to colorize the message_lines by
def colorized_message_lines(colorizer = ::RSpec::Core::Formatters::ConsoleCodes) message_lines.map do |line| colorizer.wrap line, RSpec.configuration.failure_color end end
def description
-
(String)
- The example description
def description example.full_description end
def exception
-
(Exception)
- The example failure
def exception example.execution_result.exception end
def exception_class_name
def exception_class_name name = exception.class.name.to_s name ="(anonymous error class)" if name == '' name end
def find_failed_line
def find_failed_line path = File.expand_path(example.file_path) exception.backtrace.detect do |line| match = line.match(/(.+?):(\d+)(|:\d+)/) match && match[1].downcase == path.downcase end end
def formatted_backtrace
-
(Array(String))
- the examples backtrace lines
def formatted_backtrace backtrace_formatter.format_backtrace(exception.backtrace, example.metadata) end
def fully_formatted(failure_number, colorizer = ::RSpec::Core::Formatters::ConsoleCodes)
-
(String)
- The failure information fully formatted in the way that
def fully_formatted(failure_number, colorizer = ::RSpec::Core::Formatters::ConsoleCodes) formatted = "\n #{failure_number}) #{description}\n" colorized_message_lines(colorizer).each do |line| formatted << " #{line}\n" end colorized_formatted_backtrace(colorizer).each do |line| formatted << " #{line}\n" end formatted end
def group_and_parent_groups
def group_and_parent_groups example.example_group.parent_groups + [example.example_group] end
def message_lines
-
(Array(String))
- The example failure message
def message_lines @lines ||= begin lines = ["Failure/Error: #{read_failed_line.strip}"] lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/ exception.message.to_s.split("\n").each do |line| lines << " #{line}" if exception.message end if shared_group lines << "Shared Example Group: \"#{shared_group.metadata[:shared_group_name]}\"" + " called from #{backtrace_formatter.backtrace_line(shared_group.location)}" end lines end end
def read_failed_line
def read_failed_line unless matching_line = find_failed_line return "Unable to find matching line from backtrace" end file_path, line_number = matching_line.match(/(.+?):(\d+)(|:\d+)/)[1..2] if File.exist?(file_path) File.readlines(file_path)[line_number.to_i - 1] || "Unable to find matching line in #{file_path}" else "Unable to find #{file_path} to read failed line" end rescue SecurityError "Unable to read failed line" end
def shared_group
def shared_group @shared_group ||= group_and_parent_groups.find { |group| group.metadata[:shared_group_name] } end