Fix stubbing by chained calls on collections (#565) #687
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related links
Issue: #565
Changes that affected expected behavior: #387, #536
Introduction
Any feedback is appreciated. I'm completely new to the internals of MockK, so please excuse any shortcomings of my solutions.
I recommend checking commits separately, because there are alternative solutions. I'll clean those up in case a solution is chosen.
Tests pass for both solutions.
Explanation
This is an attempt at fixing stubbing by chained calls on collection, such as:
Expected behavior broke when
(Jvm)AnyValueGenerator
started returning concrete instances for collection types. Before those branches existed, the generator would fall back toorInstantiateVia
lambda, which – when passed fromRecordingState.call
– created a temporary mock on invocation.Solutions
Solution 1: Return temporary mocks for collection types in
RecordingState.call
Encapsulating the change here helps avoid concerning other consumers of
AnyValueGenerator
, making the scope of the change much smaller than solution 2.A possible disadvantage is that these collection (or other) types have to be kept in sync between
RecordingState
andAnyValueGenerator
. Perhaps we could obtain expose those types fromAnyValueGenerator
instead?Solution 2: Add flag to configure
AnyValueGenerator
's behaviorIf interfering in
AnyValueGenerator
isn't a concern, we can pass a flag to indicate whether generated objects should be mocks. This adds more complexity than solution 1.