RSpec message expectations and verify-after-execution behavior
0 reputation · 20 Oct 2021, 13:52 UTC
RSpec provides distinct mechanisms for verifying method calls on test doubles, specifically through traditional message expectations and the use of spies. While expect().to receive() sets a requirement before the action occurs, spies enable an "act first, assert later" pattern using have_received().
There is a design consideration regarding the internal state management when transitioning from a stubbed method to a verified spy. When a double is configured with allow().to receive(), it must track all subsequent calls to support post-execution verification without altering the object's primary behavior during the execution phase.
Technical Constraints
- The verification must occur after the subject code has finished executing.
- The double must maintain a call history without interfering with the canned responses provided by the stub.
Does the have_received matcher rely on a global call history for all doubles, or is the tracking limited to objects explicitly configured with allow()? How does RSpec distinguish between a method that was never stubbed and one that was stubbed but never called during verification?