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 186
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 169
def colorized_message_lines(colorizer = ::RSpec::Core::Formatters::ConsoleCodes)
  message_lines.map do |line|
    colorizer.wrap line, RSpec.configuration.failure_color
  end
end
description() click to toggle source

@return [String] The example description

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

@return [Exception] The example failure

# File lib/rspec/core/notifications.rb, line 137
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 178
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 194
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
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 149
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

Private Instance Methods

backtrace_formatter() click to toggle source
# File lib/rspec/core/notifications.rb, line 210
def backtrace_formatter
  RSpec.configuration.backtrace_formatter
end
exception_class_name() click to toggle source
# File lib/rspec/core/notifications.rb, line 214
def exception_class_name
  name = exception.class.name.to_s
  name ="(anonymous error class)" if name == ''
  name
end
find_failed_line() click to toggle source
# File lib/rspec/core/notifications.rb, line 245
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
group_and_parent_groups() click to toggle source
# File lib/rspec/core/notifications.rb, line 224
def group_and_parent_groups
  example.example_group.parent_groups + [example.example_group]
end
read_failed_line() click to toggle source
# File lib/rspec/core/notifications.rb, line 228
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
shared_group() click to toggle source
# File lib/rspec/core/notifications.rb, line 220
def shared_group
  @shared_group ||= group_and_parent_groups.find { |group| group.metadata[:shared_group_name] }
end