Raed Ghazal
04/18/2023, 9:13 AMkotlin.Result
basically in unit tests its getting wrapped twice in a Result, so Result<String> becomes Result<Result<String>>
showing the error
class kotlin.Result cannot be cast to class String
I’m mocking the function like this
coEvery {
repo.getValues(
LOCATION,
DATE
)
} coAnswers { Result.success(DEFAULT) }
Joffrey
04/18/2023, 9:14 AMDEFAULT
here?Raed Ghazal
04/18/2023, 9:14 AMRaed Ghazal
04/18/2023, 9:14 AMRaed Ghazal
04/18/2023, 9:17 AMclass Usecase @Inject constructor(
private val repo: Repo,
) {
suspend fun call(
location: Location,
currentDate: LocalDate,
currentTime: LocalTime
): Result<String> {
return repo.getResult()
}
}
in Test class
@Before
fun setup() {
coEvery {
repo.getResult(
LOCATION,
DATE
)
} coAnswers { Result.success("ToBeReturned") }
usecase = Usecase(
repo = repo
)
}
@Test
fun `test`() = runTest {
val time = LocalTime.of(0, 0, 0)
val result = usecase.call(LOCATION, DATE, time)
Truth.assertThat(result.isSuccess).isTrue()
result.onSuccess {
Truth.assertThat(it).isEqualTo("ToBeReturned")
}
}
Raed Ghazal
04/18/2023, 9:17 AMRaed Ghazal
04/18/2023, 9:19 AMJoffrey
04/18/2023, 9:19 AMRaed Ghazal
04/18/2023, 9:19 AMJoffrey
04/18/2023, 9:37 AMephemient
04/18/2023, 1:49 PMephemient
04/18/2023, 1:52 PMephemient
04/18/2023, 1:52 PMRaed Ghazal
04/18/2023, 9:10 PM