Ir al contenido principal
Versión: 5.3.x

Matchers de Konform

[Traducción Beta No Oficial]

Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →

Kotest proporciona varios matchers para usar con Konform. Se pueden utilizar en tus pruebas para verificar que un objeto dado sea validado o falle la validación.

Para usar estos matchers, añade implementation 'io.kotest.extensions:kotest-assertions-konform:<version>' a tu configuración de compilación. Este módulo está disponible tanto para JVM como para JS.

Empecemos con una clase de datos básica:

data class UserProfile(
val fullName: String,
val age: Int?
)

Entonces, dado un validador UserProfile como este:

val validateUser = Validation<UserProfile> {
UserProfile::fullName {
minLength(4)
maxLength(100)
}

UserProfile::age ifPresent {
minimum(21)
maximum(99)
}
}

Podemos probar que las instancias pasan la validación así:

val alice = UserProfile("Alice", 25)
validateUser shouldBeValid user1

Y podemos probar que las instancias fallen la validación con mensajes de error específicos así:

val bob = UserProfile("bob", 18)
validateUser.shouldBeInvalid(a) {
it.shouldContainError(UserProfile::fullName, "must have at least 4 characters")
it.shouldContainError(UserProfile::age, "must be at least '21'")
}
MatcherDescription
validation.shouldBeValid(value)Asserts that the validation is valid for the given value
validation.shouldBeInvalid(value)Asserts that the validation is invalid for the given value
validation.shouldBeInvalid(value) { block }Asserts that the validation is invalid for the given value, and then, runs the block with invalid value