https://kotlinlang.org logo
#mockk
Title
# mockk
v

voben

02/08/2020, 8:51 PM
I’m trying to test my repo which looks something like this. The issue is I’m not quite sure how to deal with the FirebaseInstanceId. Is this something I can mock, if so how would I do that?
Copy code
class Repository(private val service: RetrofitService) {

    suspend fun sendToken() {
        val result = FirebaseInstanceId.getInstance().instanceId.await()
        service.sendTokenToServer(result.token)
    }

}
t

thanksforallthefish

02/10/2020, 6:46 AM
https://mockk.io/ -> look for
mockkStatic
😉
o

oleg_osipenko

02/10/2020, 9:30 AM
Isn't it better to provide Firebase instance as a constructor parameter? Mocking static is the evil path
💯 1
t

thanksforallthefish

02/10/2020, 9:42 AM
I agree, I assumed it was a dummy example and it might not be straight forward to do proper DI. if it was for me, I would remove mockkstatic, though it is needed for extension function I guess
👍 1
v

voben

02/10/2020, 3:43 PM
So you’re saying inject the firebase instance and mockStatic the await function.
k

kyleg

02/11/2020, 6:24 AM
That’s what I would do, too.
g

gildor

02/19/2020, 3:41 PM
I wouldn't mock static await function, looks as very bad idea in general (and even note sure why do you need this, you can just return own Task instance). And wouldn't even inject FirebaseInstanceId, just abstract it and have own interface that provides instance id This approach not only allows you to test it, doesn't require mocking at all, but also follows separation of concerns principle
3 Views