Asbertequals无法实现错误,但成功通过
我会有这些密封的接口
sealed interface Result<out T> {
data class Success<T>(val data: T) : Result<T>
data class Error(val exception: Throwable? = null) : Result<Nothing>
}
当我尝试assertequals
成功的界面时, 。但是,当涉及到错误时,即使内容相同,它也会失败。这是一个简单的示例:
@Test
fun testSucess() = runTest {
whenever(repository.login("email", "password"))
.thenReturn(someValue)
val expected = Result.Success(data = someValue)
val actual = loginUseCase(LoginRequest("email", "password"))
verify(repository).login("email", "password")
assertEquals(expected, actual) // this will pass
}
@Test
fun testError() = runTest {
val exception = RuntimeException("HTTP Error")
whenever(repository.login("", ""))
.thenThrow(exception)
val expected = Result.Error(exception = exception)
val actual = loginUseCase(LoginRequest("", ""))
verify(repository).login("", "")
assertEquals(expected, actual) // this will fail
assertEquals(expected.toString(), actual.toString()) // this will pass
}
什么是导致此事的,什么可能解决的方法?我已经阅读了一些需要equals()
的信息,但是我仍然感到困惑为什么仅在错误情况下发生,以及如何正确覆盖等于等于方法。
I have these sealed interface
sealed interface Result<out T> {
data class Success<T>(val data: T) : Result<T>
data class Error(val exception: Throwable? = null) : Result<Nothing>
}
when i tried to assertEquals
the Success one, it pass. But when it comes to Error one, it will fail even though the content is identical. Here is simple example:
@Test
fun testSucess() = runTest {
whenever(repository.login("email", "password"))
.thenReturn(someValue)
val expected = Result.Success(data = someValue)
val actual = loginUseCase(LoginRequest("email", "password"))
verify(repository).login("email", "password")
assertEquals(expected, actual) // this will pass
}
@Test
fun testError() = runTest {
val exception = RuntimeException("HTTP Error")
whenever(repository.login("", ""))
.thenThrow(exception)
val expected = Result.Error(exception = exception)
val actual = loginUseCase(LoginRequest("", ""))
verify(repository).login("", "")
assertEquals(expected, actual) // this will fail
assertEquals(expected.toString(), actual.toString()) // this will pass
}
What is causing this and what is possible solution to this? I have read some info that it needs equals()
to be overriden, but i still confused as to why it only happens in Error case only and how to properly override the equals method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
kotlin中的数据类具有隐式生成的
equals
源自其所有属性。您面临的问题可能是由于您的
someValue
的类型具有适当的equals
函数,因此Equals适用于您的Success
及其属性value
。但是投掷
没有等于
函数,这意味着两个可投掷
s仅在它们是同一实例的情况下等于预期
和实际
的情况。我只能猜测,在loginusecase
中,该异常被包裹在另一个异常中,还是基于存储库抛出的一个创建一个新的异常?kotlin已经有一个内置
result>结果 ,我强烈建议使用它而不是定义自己的。
尽管如此,如果您使用内置类型,则可能会遇到相同的问题,因为
equals
检查对于不同的异常实例仍然失败。有几种方法可以解决:
预期的是错误
(或使用默认的结果
键入enduce> enduce> endure> firese.firess.isfure
),然后检查消息是否相同。loginusecase
抛出与存储库所抛出的异常实例完全相同。Data classes in Kotlin have an implicitly generated
equals
function automatically derived from all their properties.The problem you are facing is probably due to the fact that the type of your
someValue
has a properequals
function, so the equals works for yourSuccess
and its propertyvalue
. ButThrowable
does not have anequals
function which means that twoThrowable
s are only equal if they are the same instance, which is obviously not the case forexpected
andactual
in your test assertion. I can only guess that inloginUseCase
, the exception is wrapped inside another exception, or a new exception is created based on the one thrown by the repository?Kotlin already has a built-in
Result
type, and I strongly recommend using that one instead of defining your own.Nonetheless, if you use the built-in type, you will probably face the same problem, since the
equals
check still fails for the different exception instances.There are several ways to solve that:
expected is Error
(or with the defaultResult
type thatexpected.isFailure
), and then check that the messages are the same.loginUseCase
throws exactly the same exception instance as is thrown by the repository.