Remon Shehata
04/16/2022, 8:19 PMsealed class LoginResult {
object Success : LoginResult()
object WrongCredentials : LoginResult()
data class InvalidData(val error: ErrorType) : LoginResult()
}
sealed class ErrorType {
object EmptyEmail : ErrorType()
object InvalidEmailFormat : ErrorType()
object EmptyPassword : ErrorType()
}
how can I assert that I got the result of InvalidData
with the error of EmptyEmail
?Landry Norris
04/16/2022, 8:23 PMerror is EmptyEmail
Remon Shehata
04/16/2022, 8:24 PMassertTrue(result is LoginResult.InvalidData)
Remon Shehata
04/16/2022, 8:24 PMRemon Shehata
04/16/2022, 8:25 PMval x = result as LoginResult.InvalidData
assertTrue(x.error is ErrorType.EmptyEmail)
Remon Shehata
04/16/2022, 8:25 PMRick Clephas
04/16/2022, 8:27 PMassertIs
to type check the result
. This will smart cast result
so you can directly access error
Rick Clephas
04/16/2022, 8:29 PMassertIs<LoginResult.InvalidDate>(result)
assertIs<ErrorType.EmptyEmail>(result.error)
https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-is.htmlRemon Shehata
04/16/2022, 8:33 PMRemon Shehata
04/16/2022, 8:33 PMRick Clephas
04/16/2022, 8:36 PMtestImplementation(kotlin("test"))
Remon Shehata
04/16/2022, 8:41 PM