2

I have a collection of objects:

data class WeatherForecast(
    val city: String,
    val forecast: String
    // ...
)

I would like to test that each and every item matches given predicate on field.

Is there any assertion in kotlintest assertions that will allow me to do so?

Something like:

 forecasts.eachItemshouldMatch{ it.forecast == "SUNNY" }
sksamuel
  • 16,154
  • 8
  • 60
  • 108
pixel
  • 24,905
  • 36
  • 149
  • 251
  • As far as I can see in the [documentation](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/) of `kotlin.test` there seems to be no way to collect the result of multiple assertions. With JUnit5 you would be able to use `assertAll`. – Endzeit Nov 27 '21 at 00:02

2 Answers2

2

What about using an inspector.

list.forAll {
  it.forecast shouldBe "SUNNY"
}

https://kotest.io/docs/assertions/inspectors.html

sksamuel
  • 16,154
  • 8
  • 60
  • 108
-2

You can simply use the all function; i.e.:

forecasts.all { it.forecast == "SUNNY" }
Schottky
  • 1,549
  • 1
  • 4
  • 19