mock: add new AnythingImplementing function to check if values implement interfaces#1853
Open
GCrispino wants to merge 3 commits intostretchr:masterfrom
Open
mock: add new AnythingImplementing function to check if values implement interfaces#1853GCrispino wants to merge 3 commits intostretchr:masterfrom
GCrispino wants to merge 3 commits intostretchr:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The
mock.AnythingImplementingfunction is added such that we can check if arguments used in mocks implemented a certain interface.Example of usage (see the Motivation section for more background on why I think this is useful):
Changes
AnythingImplementingfunctionanythingImplementingprivate type for doing the type checksDifffunctionDiff'sswitch expected := expected.(type)type switch to cover the use ofAnythingImplementing. If a mismatch has been detected, the error format string%s\t%d: FAIL: value of type %T does not implement interface %s\nis included in the final error stringTest_Mock_AssertCalled_WithAnythingImplementingArgument: tests successful caseTest_Arguments_Diff_WithAnythingImplementingArgument: tests the use ofDiffwhere no mismatch has been detectedTest_Arguments_Diff_WithAnythingImplementingArgument_Failing: tests the format of the error string in the case thata mismatch has been detected in
DiffMotivation
If we want to mock a function that takes a value of a certain interface type, to the best of my knowledge, there's nothing in testify for checking if an argument implements a certain interface.
As far as I know, the closest is
AnythingOfType, but then we need to know which concrete type is being used as argument to the function to use it.Example
Consider the following example of the
Callerinterface, which only has a functionCallthat takes a value of thecontext.Contextinterface type:When testing it using mocks and
mock.AnythingOfType, we need to know the concrete type of the argument being received byCall, doing something like the following:We need to know that
MustCallusescontext.backgroundCtx, which can be a bit fragile. For example,MustCallcould use different concrete types depending on some logic, which would make checking the mocks a bit more brittle.With
mock.AnythingImplementing, we can just check against thecontext.Contextinterface, which is cleaner:Checking against a type that is not implemented by the argument provided in the MustCall function will make the test fail:
This is the failure shown in the test: