Skip to content

Fix stub_const for case that const_missing defines target nested constant

Created by: sorah

Added const_get before calling const_defined because Module#const_missing may define target constant.

Representive case is a Ruby on Rails (ActiveSupport), https://github.com/rails/rails/blob/master/activesupport/lib/active_support/dependencies.rb#L176

In test in a Rails app, you'll get an error if you used stub_const for controllers, models, helpers, etc before it get loaded by ActiveSupport::Dependencies::ModuleConstMissing#const_missing.

  1. stub_const "FooController::X", :foo
  2. rspec-mocks defines FooController as empty module
  3. call FooController
  4. Normally, const_missing has called and Rails loads the controller, but rspec-mocks defined FooController in stub_const, so controller won't be loaded.
  5. You'll get an error.

Merge request reports