Need some help testing Room + coroutines + SharedF...
# android
a
Need some help testing Room + coroutines + SharedFlow on Robolectric. Any ideas? 🙏
c
Hi @azabost can you share a repository with a minimal implementation? I think it would be easier to help and provide feedback I think you need an
testScheduler.advanceUntilIdle()
after the
runCurrent()
but I can't try it because I need to implement all that 🤣
a
Hey.
advanceUntilIdle()
didn't help. I think the problem was with how Room executes the queries, e.g. it runs them on some arbitrary
Executor
and the test coroutine is not able to wait until it completes. I was able to work around the problem by using a combination of both: • InstantTaskExecutorRuleallowMainThreadQueries() I also followed this issue on GitHub where one of the problems being discussed is this:
🙌🏽 1
c
and that's why I avoid this integration tests 🤣 well, I'm glad you managed to solve it! but I think the
Executor
is not always arbitrary, you can set your own with the
DatabaseConfiguration
on
init()
if I'm not wrong 🤔
a
Yes, you can, but would it be more helpful than
InstantTaskExecutorRule
?
Btw I value these integration tests as they let me check my DB queries etc. are correct, and sometimes they aren't so straightforward 🙂
c
I mean, if you can have it tested is great! but for simple things I test them manually, instead of dealing with all the configuration, but, I'm that lazy 🤣 I don't know which one would be better, I only mentioned the other one because it caught my attention that you need to configure the
allowMainThreadQueries()
a
Yeah, I need to
allowMainThreadQueries
because
InstantTaskExecutorRule
returns a
TaskExecutor
with
Copy code
@Override
public boolean isMainThread() {
    return true;
}
so Room thinks I query it on the main thread 🤦‍♂️ and throws exceptions btw for safety I set
allowMainThreadQueries
only in tests, not in production code.
👍🏽 1
c
Sure, but it depends on how you are setting up your tests. if it's just the repository interface, there would be no problem. But if you do it from the use case (like an acceptance test), letting them call you from the main thread can be a problem