Hey, I’m facing a bug in kotlin while testing the ...
# coroutines
r
Hey, I’m facing a bug in kotlin while testing the
kotlin.Result
basically in unit tests its getting wrapped twice in a Result, so Result<String> becomes Result<Result<String>> showing the error
Copy code
class kotlin.Result cannot be cast to class String
I’m mocking the function like this
Copy code
coEvery {
    repo.getValues(
        LOCATION,
        DATE
    )
} coAnswers { Result.success(DEFAULT) }
j
What is
DEFAULT
here?
r
you can think of it as a String, its just the same type as what the function returns
a const value of the result
Copy code
class Usecase @Inject constructor(
    private val repo: Repo,
) {

    suspend fun call(
        location: Location,
        currentDate: LocalDate,
        currentTime: LocalTime
    ): Result<String> {
        return repo.getResult()
    }
}
in Test class
Copy code
@Before
fun setup() {
    coEvery {
        repo.getResult(
            LOCATION,
            DATE
        )
    } coAnswers { Result.success("ToBeReturned") }

    usecase = Usecase(
        repo = repo
    )
}
Copy code
@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")
    }

}
this basically represent the case
so I just tried that instead of mocking, I implemented a fake test repo, and it worked, but apparently with mocking this doesn’t work found multiple issues open for the same bug https://youtrack.jetbrains.com/issue/KT-47206 https://github.com/mockk/mockk/issues/485 but they are kind of old
j
It'd be nice if you could share minimal code that reproduces the behaviour you're seeing
j
☝️ this doesn't compile and doesn't have the Repo class
e
I'm pretty sure this is entirely a problem with mockk and value classes in general
r
ah, yeah it can be the case, implementing a fake repo that returns the fake data worked, the data is not going to change each test case so a fake repo works for my case as well alright then, thanks both!