Skip to content

Inconsistent use of #inspect in expect_raises #14030

@HertzDevil

Description

@HertzDevil

When an expect_raises assertion fails, the expected message goes through #pretty_inspect but the actual message doesn't:

ex_to_s = ex.to_s
case message
when Regex
unless (ex_to_s =~ message)
backtrace = ex.backtrace.join('\n') { |f| " # #{f}" }
fail "Expected #{klass} with message matching #{message.pretty_inspect}, " \
"got #<#{ex.class}: #{ex_to_s}> with backtrace:\n#{backtrace}", file, line
end
when String
unless ex_to_s.includes?(message)
backtrace = ex.backtrace.join('\n') { |f| " # #{f}" }
fail "Expected #{klass} with #{message.pretty_inspect}, got #<#{ex.class}: " \
"#{ex_to_s}> with backtrace:\n#{backtrace}", file, line
end
when Nil
# No need to check the message
end

This leads to some confusing situations where the two messages look identical if, for example, the actual message is already the #inspect form of the expected message:

require "spec"

it do
  expect_raises(Exception, %q(a\tb\nc)) do
    raise %q(a\tb\nc).inspect
  end
end
Failures:

  1) assert
     Failure/Error: expect_raises(Exception, %q(a\tb\nc)) do

       Expected Exception with "a\\tb\\nc", got #<Exception: "a\\tb\\nc"> with backtrace:
...

We should make the failure less ambiguous here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions