Mocking multiple types
To create a mock of multiple types use mockMany:
val mock = mockMany<A, B, C>()
mockMany accepts up to 5 types. These types have to satisfy given requirements:
- Each type must be supported.
- Types must not duplicate (e.g. passing
List<String>andList<Int>is duplication). - Only one class is allowed.
- Functional types are not supported on JS.
mockMany returns MockManyN type that is a marker for multiple types mock. To access member functions from specific type,
explicit cast is required. You can achieve it with tN extensions:
val mock = mockMany<A, B, C> {
// t1 extension casts to A
every { t1.functionFromA() } returns Unit
// t2 extension casts to B
every { t2.functionFromB() } returns Unit
// t3 extension casts to C
every { t3.functionFromC() } returns Unit
}
val foo = Foo(a = mock.t1)
Shared functions
If types share function with the same signature, they are treated as one:
interface A {
// ...
fun sharedFunction(i: Int): String
}
interface B {
// ...
fun sharedFunction(i: Int): String
}
// ...
val mock = mockMany<A, B> {
every { t1.sharedFunction(any()) } returns "Hello world!"
}
// ...
mock.t2.sharedFunction(1) // returns "Hello world!"