Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#617·mock

Adding method to Controller allowing to check whether all expected calls have been made

Author: FlorianLochCreated Feb 7, 2022Updated Apr 14, 2023
Labelstype: feature request

I want to be able to check whether all expected calls have been made before Controller#Finish() gets invoked as the code being tested is running asynchronously and I can only approximate when all calls will have been made.

This addition should allow it to check whether all expected calls towards the mock(s) have been made in an idempotent way. This is useful when testing asynchronous/non-deterministic things that aren't fully under the developers control (e.g., in end-2-end tests).

If necessary I can elaborate on my specific testing scenario.

Having such a method allows the user to write something like

go
require.Eventually(t, mockCtrl.AllExpectedCallsSatisfied, 60*time.Second, 100*time.Millisecond)

instead of having to wait for an approximated amount of time (which slows down the tests unnecessarily) in order to make sure all expected calls have been satisfied before Controller#Finish() gets invoked.

Why is this needed? Because right now AFAIU I have to add a sleep for an amount of time I approximate to be enough for my system to be done with its work. This is not nice for several reasons:

  • It's dangerous because my estimation could be too little and calls have not been made at that point
  • It's slow because I basically have to approximate very conservatively due to the problem described above

In order to check how feasible the implementation would be I already coded a proposal that I would be happy to open a PR for: https://github.com/golang/mock/compare/main...FlorianLoch:check-expectation-fulfilled?expand=1

Source: golang/mock

View original on GitHubView discussion on GitHub