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
this PR adds
AtLeast(i int)
to mocked objects, allowing tests to require that a function is called at leasti
times.Changes
all in the mock package:
AtLeast(i int)
to expose the featureminimumCalls int
to record the value passed from the original callercheckExpectation
to compare the number of calls to the minimum number of expected callsMotivation
this change is necessary to know if a function has been called at least N times.
this is mostly useful when the count of function execution can't easily be determined (looking at you
time.Sleep
), so.Twice()
or.Times(N)
can't be used effectively.this leads to code that instead uses
.Maybe()
and then later.AssertCalled()
to make sure it was called at least once - but makes the logic a bit disconnected and provides no way to specify a reasonable number of times the function should at least be called.here's a contrived example on how it can be used:
something.go
something_test.go
Problems
currently
AssertExpectations
specifies that every failure coming fromcheckExpectation
indicates that one more call is necessary - which with this change will not always be true, and can lead to confusion.i don't have a good solution to this problem, but am very open to feedback.
in the above example, you'd see output similar to:
but in fact, the code being tested needs to make 2 more calls
additionally, while it doesn't matter in the contrived example or my first actual use case, setting
Repeatability
to0
and always returning a value from the function could cause undesired behaviors in others.