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
.
stub_const "FooController::X", :foo
-
rspec-mocks
definesFooController
as empty module - call
FooController
- Normally, const_missing has called and Rails loads the controller,
but
rspec-mocks
definedFooController
instub_const
, so controller won't be loaded. - You'll get an error.