instance_double interface validation with metaprogramming extensions
0 reputation · 28 Aug 2023, 01:17 UTC
Verified Double Contract Enforcement
RSpec's instance_double and class_double provide a mechanism to ensure mocks remain synchronized with the actual implementation by verifying method signatures against the underlying class. This prevents tests from passing when the source API has drifted.
Dynamic Method Resolution
A challenge arises when the target class utilizes Ruby metaprogramming to define methods dynamically at runtime or via module extensions. Because verified doubles rely on reflection against the loaded class, there is uncertainty regarding how RSpec handles methods that are not explicitly defined in the class body but are available via method_missing or dynamic injection.
When transitioning from permissive double usage to strict contract-based testing, the behavior for these dynamic interfaces remains inconsistent.
- Does
instance_doublerecognize methods added viaextendorincludeafter the class is initially defined? - How can verified doubles be configured to validate methods defined via
method_missingwithout reverting to a generic double?