Lukasz Kalnik
07/25/2025, 4:29 PMforEach
loop with lambdas containing mocks?Lukasz Kalnik
07/25/2025, 4:35 PMenum class TestScenario {
NoLocation,
NoMarket,
MultipleMarkets,
}
fun mockMarketByLocation(scenario: TestScenario) {
when (scenario) {
NoLocation -> every { LocationRepository.getLocation() } returns null
NoMarket -> {
every { LocationRepository.getLocation() } returns mockLocation
every { MarketRepository.getMarket(mockLocation) } returns null
}
// ...
}
}
class MarketByLocationTest : StringSpec({
withData(NoLocation, NoMarket, MultipleMarkets) { scenario ->
mockMarketByLocation(scenario)
MarketSearchViewModel().getMarket() shouldBe MarketNotFound
}
})
Alex Kuznetsov
07/25/2025, 7:41 PMsam
07/25/2025, 7:48 PMsam
07/25/2025, 7:48 PMVaibhav Jaiswal
07/28/2025, 4:27 AMcontext
and multiple test
blocks in itLukasz Kalnik
07/28/2025, 6:39 AMwithData()
.Lukasz Kalnik
07/28/2025, 6:42 AMwithData(
{ every { LocationRepository.getLocation() } returns null },
{
every { LocationRepository.getLocation() } returns mockLocation
every { MarketRepository.getMarket(mockLocation) } returns null
},
// ...
) { setup ->
setup()
// test body
}
sam
07/28/2025, 6:43 AMsam
07/28/2025, 6:44 AMLukasz Kalnik
07/28/2025, 6:44 AMLukasz Kalnik
07/28/2025, 6:44 AMsam
07/28/2025, 6:48 AMLukasz Kalnik
07/28/2025, 9:42 AMval noUserLocation = { coEvery { FusedLocationRepository.getUserLocation() } returns null }
and then passing a map to withData()
val locationSetups = mapOf(
"no user location" to noUserLocation,
// ...
)
withData(locationSetups) { setup ->
setup()
// Test body
}
Alex Kuznetsov
07/28/2025, 1:22 PMLukasz Kalnik
07/28/2025, 1:25 PMLukasz Kalnik
07/28/2025, 1:27 PMFusedLocationRepository
and MarketRepository
?Alex Kuznetsov
07/28/2025, 1:29 PMLukasz Kalnik
07/28/2025, 1:29 PMAlex Kuznetsov
07/28/2025, 1:33 PMLukasz Kalnik
07/28/2025, 1:33 PMLukasz Kalnik
07/28/2025, 1:33 PMLukasz Kalnik
07/28/2025, 1:34 PMAlex Kuznetsov
07/28/2025, 1:34 PMLukasz Kalnik
07/28/2025, 1:34 PMLukasz Kalnik
07/28/2025, 1:34 PMAlex Kuznetsov
07/28/2025, 1:35 PM