Hey got a question about testing. Suppose I have ...
# apollo-kotlin
s
Hey got a question about testing. Suppose I have a simple UseCase, that receives an apolloClient in its constructor.It has one method that calls one query, and does some business logic with it and returns something. Is there no way to simply mock the ApolloClient to just return a specific object? I’ve read this article for example but it feels quite involved for what I want to do. Optimally I’d like to be able to do inside a test class something like:
Copy code
private val mockedApolloClient = mockk<ApolloClient>().apply {
    val apolloClient = this
    coEvery { apolloClient.query(MyCustomQuery()/* or even omit this if possible?*/) } coAnswers {
        MyObject()
    }
}
private val useCase = MyUseCase(mockedApolloClient)

@Test
fun someTest() {
    assert(useCase(input) == SomeResult)
}
I tried looking into the documentation and I didn’t find anything out unfortunately. Note that we’re still on 2.x.x but if a better solution exists for 3.0.0 I’d be happy to hear that as we’ll migrate to it soon anyway.
Note that I am not fixed on having this mocked no matter what. If mocking isn’t the way to go, or if there’s a well-known better approach to this or anything, I’d love to read more about it! It’s just that in the documentation under “Testing”, UI Tests are covered which isn’t what I’m looking for in this case, and then Test builders, which aren’t what I’m looking for here either… I think?
m
It's more or less the same approach as the article you linked from what I understand, i.e. provide a fake network transport that returns in-memory objects instead of doing a full HTTP roundtrip
s
Oh yeah that would be amazing actually! Sounds like exactly what I would be looking for, and even nicer than my initial idea!
m
👍 we can schedule this for the next release. With the Holiday break, that might not happen before January though
s
After the huge 3.0.0 release too, you all deserve a break, absolutely!
💙 2
☝️ 1