How can we mock `FirebaseAuth.getInstance().signIn...
# android
k
How can we mock
FirebaseAuth.getInstance().signInWithCustomToken(token).addOnCompleteListener{}
and other Task like
addOnSuccessListener
and
addOnFailureListener
using MockK ?
s
I used to do a similar thing like this:
Copy code
@MockK(relaxed = true)
private lateinit var mockTask: Task<List<FirebaseVisionBarcode>>
Copy code
every { mockTask.addOnFailureListener(any()) } answers {
   firstArg<OnFailureListener>().onFailure(mockk())
   mockTask
}
To mock invoke a failure
👍 1
m
Don't mock tasks
return real tasks, for example
Tasks.forException(...)
👆 1
k
for
FirebaseAuth.getInstance().signInWithCustomToken(token).addOnCompleteListener
i have created
Copy code
lateinit var mockAuthTask: Task<AuthResult>
...
beforeEachTest{
.....
every { mockAuthTask.addOnSuccessListener(any()) } answers { firstArg<OnCompleteListener<AuthResult>>().onComplete(mockk()) }
}
but its not working how can i do it @Samme Kleijn
@Matej Drobnič can you please give me more info
?
m
instead of whole mock stuff, just create dummy AuthResult and create real task:
Copy code
val result = AuthResult(...)
val authTask = Tasks.forResult(result)
k
@Matej Drobnič but AuthResult is interface i don't know how to implement that class as Concrete
for this do i also have to update real code(UseCases,Repositories) or just tests? @Matej Drobnič, because i'm not in situation to update real UseCases or repositories i am just fixing mocking issues
actually i have tried as @Samme Kleijn suggested, but i'm getting this
Failure(e=io.mockk.MockKException: no answer found for: Task(#17).getException())
i think i'm getting this because we are sending
AuthResult
as mockobject but now i don't know how to solve it 😞
m
just tests
you can use
mockk()
instead of implementing AuthResult I guess
or you can create your own
FakeAuthResult
class that implements the interface
k
@Matej Drobnič actually
mock(relaxed=true)
worked
but this is mocked, but as you said don't mock tasks and return real, what is Tasks.forExceptions() and how can i return real tasks, is it going to connect to connect Firebase server and return real tasks ? @Matej Drobnič
m
Task is just a wraper that can return either result or exception
it will not go to firebase
Tasks.forResult(mockk())
is a good compromise
you don't mock the task, but you mock real value
but it would be even better if you don't mock anything and just return concrete AuthResult
👍 1
k
Thanks @Matej Drobnič & @Samme Kleijn, both of yours solutions really helped me.
👍 1
after working everything currently i'm getting
Copy code
no answer found for: SomeClass(#3).setSomeVariable(dummy)
how can i add
every
for setters of fields ?
l
280 Views