class RSpec::Core::Notifications::FailedExampleNotification

The `FailedExampleNotification` extends `ExampleNotification` with things useful for failed specs.

@example

def example_failed(notification)
  puts "Hey I failed :("
  puts "Here's my stack trace"
  puts notification.exception.backtrace.join("\n")
end

@attr [RSpec::Core::Example] example the current example @see ExampleNotification

Public Instance Methods

colorized_formatted_backtrace(colorizer=::RSpec::Core::Formatters::ConsoleCodes) click to toggle source

Returns the failures colorized formatted backtrace.

@param colorizer [#wrap] An object to colorize the #message_lines by @return [Array<String>] the examples colorized backtrace lines

# File lib/rspec/core/notifications.rb, line 190
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
colorized_message_lines(colorizer=::RSpec::Core::Formatters::ConsoleCodes) click to toggle source

Returns the message generated for this failure colorized line by line.

@param colorizer [#wrap] An object to colorize the #message_lines by @return [Array<String>] The example failure message colorized

# File lib/rspec/core/notifications.rb, line 173
def colorized_message_lines(colorizer=::RSpec::Core::Formatters::ConsoleCodes)
  add_shared_group_lines(failure_lines, colorizer).map do |line|
    colorizer.wrap line, message_color
  end
end
description() click to toggle source

@return [String] The example description

# File lib/rspec/core/notifications.rb, line 158
def description
  example.full_description
end
exception() click to toggle source

@return [Exception] The example failure

# File lib/rspec/core/notifications.rb, line 153
def exception
  example.execution_result.exception
end
formatted_backtrace() click to toggle source

Returns the failures formatted backtrace.

@return [Array<String>] the examples backtrace lines

# File lib/rspec/core/notifications.rb, line 182
def formatted_backtrace
  backtrace_formatter.format_backtrace(exception.backtrace, example.metadata)
end
fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::ConsoleCodes) click to toggle source

@return [String] The failure information fully formatted in the way that

RSpec's built-in formatters emit.
# File lib/rspec/core/notifications.rb, line 198
def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::ConsoleCodes)
  "\n  #{failure_number}) #{description}\n#{formatted_message_and_backtrace(colorizer)}"
end
message_lines() click to toggle source

Returns the message generated for this failure line by line.

@return [Array<String>] The example failure message

# File lib/rspec/core/notifications.rb, line 165
def message_lines
  add_shared_group_lines(failure_lines, NullColorizer)
end

Private Instance Methods

add_shared_group_lines(lines, colorizer) click to toggle source
# File lib/rspec/core/notifications.rb, line 235
def add_shared_group_lines(lines, colorizer)
  example.metadata[:shared_group_inclusion_backtrace].each do |frame|
    lines << colorizer.wrap(frame.description, RSpec.configuration.default_color)
  end

  lines
end
backtrace_formatter() click to toggle source
# File lib/rspec/core/notifications.rb, line 213
def backtrace_formatter
  RSpec.configuration.backtrace_formatter
end
encoding_of(string) click to toggle source
# File lib/rspec/core/notifications.rb, line 205
def encoding_of(string)
  string.encoding
end
exception_class_name() click to toggle source
# File lib/rspec/core/notifications.rb, line 217
def exception_class_name
  name = exception.class.name.to_s
  name = "(anonymous error class)" if name == ''
  name
end
failure_lines() click to toggle source
# File lib/rspec/core/notifications.rb, line 223
def failure_lines
  @failure_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
      lines
    end
end
find_failed_line() click to toggle source
# File lib/rspec/core/notifications.rb, line 261
def find_failed_line
  example_path = example.metadata[:absolute_file_path].downcase
  exception.backtrace.find do |line|
    next unless (line_path = line[/(.+?):(\d+)(|:\d+)/, 1])
    File.expand_path(line_path).downcase == example_path
  end
end
formatted_message_and_backtrace(colorizer) click to toggle source
# File lib/rspec/core/notifications.rb, line 269
def formatted_message_and_backtrace(colorizer)
  formatted = ""

  colorized_message_lines(colorizer).each do |line|
    formatted << RSpec::Support::EncodedString.new("     #{line}\n", encoding_of(formatted))
  end

  colorized_formatted_backtrace(colorizer).each do |line|
    formatted << RSpec::Support::EncodedString.new("     #{line}\n", encoding_of(formatted))
  end

  formatted
end
message_color() click to toggle source
# File lib/rspec/core/notifications.rb, line 283
def message_color
  RSpec.configuration.failure_color
end
read_failed_line() click to toggle source
# File lib/rspec/core/notifications.rb, line 243
def read_failed_line
  matching_line = find_failed_line
  unless matching_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