Custom matching functions #

When built-in argument matchers aren’t sufficient, custom matchers can be written. A custom argument matcher simply needs to take in the argument as a parameter and return true if it matches, and false if it does not. This custom boolean function can then be passed to the special match function.

every {
  mockedCar.drive(match { engine ->
    engine.type === "Diesel"
  })
} returns true

If the argument is nullable, a variant called matchNullable can be used instead.

every {
  mockedCar.drive(matchNullable { engine ->
    engine != null && engine.type === "Diesel"
  })
} returns true

Matcher subclass #

TODO extend Matcher<T>

Inline extension function helper #

TODO MockKMatcherScope extension function

Buy Me a Coffee at ko-fi.com