Skip to content

Add test coverage for NameError#missing_name branches - #58325

Merged
byroot merged 1 commit into
rails:mainfrom
hammadxcm:test-name-error-missing-name-branches
Aug 1, 2026
Merged

byroot merged 1 commit into
rails:mainfrom
hammadxcm:test-name-error-missing-name-branches

Conversation

@hammadxcm

Copy link
Copy Markdown
Contributor

Summary

NameError#missing_name branches on the exception's receiver:

receiver = begin
  self.receiver
rescue ArgumentError
  nil
end

if receiver == Object
  name.to_s
elsif receiver
  "#{real_mod_name(receiver)}::#{self.name}"
else
  if match = message.match(/((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/)
    match[1]
  end
end

Of these, only the elsif receiver branch and the early return for non-constant errors were exercised. Line coverage over activesupport/test/core_ext/ showed the receiver == Object branch, the rescue ArgumentError branch, and the message-matching fallback all unreached.

Notably the receiver == Object case is the example in the method's own documentation:

begin
  HelloWorld
rescue NameError => e
  e.missing_name
end
# => "HelloWorld"

Tests added

  • test_missing_top_level_constant_is_not_namespaced — a missing top-level constant has Object as its receiver and returns the name unqualified, matching the documented example.
  • test_missing_name_falls_back_to_the_message_without_a_receiver — a NameError built without a receiver raises ArgumentError from #receiver, so the name is recovered from the message.
  • test_missing_name_ignores_a_message_that_is_not_about_a_constant — missing_name is nil and missing_name? is false for a non-constant message.

No behavior changes — tests only, so no CHANGELOG entry.

active_support/core_ext/name_error.rb goes from 17/21 to full line coverage. activesupport/test/core_ext/: 1474 runs, 6124 assertions, 0 failures, 0 errors. RuboCop clean.

`NameError#missing_name` has four code paths depending on the exception's
receiver, but only the namespaced one (`receiver` is a module other than
`Object`) and the early return for non-constant errors were covered.

Adds coverage for:

* A missing top-level constant, where `receiver` is `Object` and the name
  is returned unqualified. This is the example given in the method's own
  documentation.
* An exception with no receiver, where `#receiver` raises `ArgumentError`
  and the name is recovered from the message instead.
* `missing_name?` returning false for a message that is not about an
  uninitialized constant.

This brings `active_support/core_ext/name_error.rb` to full line coverage.
@byroot
byroot merged commit dd1c884 into rails:main Aug 1, 2026
3 checks passed
@hammadxcm
hammadxcm deleted the test-name-error-missing-name-branches branch August 1, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants