Allow mockCreate/mockUpdate to receive a matching function #263
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.
tl;dr Allow
match
to accept a function or attribute(s) to determine whether a create or update matched.The function passed to
match
is given therequestData
and can perform bespoke logic to determine whether a request matched or not. If the function returnstrue
then a match is recorded, otherwise it is not.We have a use-case where we're updating a complex, nested model called
Parent
.Parent
hasManyChild
relationships, each of which has multiple attributes. We're mocking an update toParent
but what we really care about is one change to an attribute ofChild
, let's saydescription
. Using the existingmatch
function we would need to pass in the entire child structure, which is quite unwieldy. By allowingmatch
to receive a function the responsibility of determining whether a match was made belongs to the test, e.g.,:One thing I don't like about this approach is that the caller needs to be aware of the adaptor type and process the
requestData
themselves to do the match. I think it's a reasonable compromise to get the flexibility of performing arbitrary matching.I've given a stab at writing documentation and tests for this functionality. Happy to start a discussion and rework this PR if it seems like a sensible addition.