문제

Has anyone had any luck writing unit tests for AsyncTaskLoaders in Robolectric? I tried a modified version of this example for AsyncTask, but it doesn't exhibit the same behavior. In particular onLoadFinished is never called. Are there any workable examples out there for testing Loaders with Robolectric?

도움이 되었습니까?

해결책

Don't use Loaders. They aren't easy to test, and they're tightly coupled to the Fragments and Activities that use them. Instead, use RxJava Observables for your asynchronous data flows. You can de-couple the scheduling and test them running synchronously in your unit tests. It's much cleaner, and much more straightforward.

다른 팁

I just posted a way i got it to work in this stack ticket: Testing a CursorLoader with Robolectric & Mockito

It involves writing a couple extra shadow classes. I can paste the fix here if requested but I wasn't sure what the convention was for a duplicate answer.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top