使用 robolectric 版本 4.5.1
和有什么区别
shadowOf(getMainLooper()).idleFor(1, TimeUnit.MILLISECONDS);
和
shadowOf(getMainLooper()).idle();
shadowOf(getMainLooper()).idle(); 导致我的测试用例失败,给出以下异常消息
Main looper has queued unexecuted runnables. This might be the cause of the test
failure. You might need a shadowOf(getMainLooper()).idle() call.
java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
at org.robolectric.android.internal.AndroidTestEnvironment.checkStateAfterTestFailure(AndroidTestEnvironment.java:502)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:581)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:278)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
但是 shadowOf(getMainLooper()).idleFor(1, TimeUnit.MILLISECONDS); 这很好用吗?
作为文档 idleFor 将系统时钟提前给定时间,然后执行在给定时间之前或在给定时间安排的所有已发布任务。这是什么意思推进系统时钟?我的测试是如何通过使用 idleFor() 而不是 idle()