2013-02-23 18 views

risposta

15

Chiamare execute(...) sull'attività, quindi attendere il risultato chiamata Robolectric.runBackgroundTasks()/Robolectric.flushBackgroundThreadScheduler() quindi è possibile affermare.

@Test 
public void test() { 
    //create task 
    MyAsyncTask asyncTask = new MyAsyncTask(); 

    //start task 
    asyncTask.execute(...); 

    //wait for task code 
    // Robolectric.runBackgroundTasks(); (pre 3.0) 
    Robolectric.flushBackgroundThreadScheduler(); //from 3.0 

    //can run asserts on result now 
    assert...(asyncTask.get()); 
} 
+0

Grazie! Questa è la soluzione ... – StefMa

+0

@bianca questa risposta è accettabile? – weston

7

Con Robolectric 2.4 è adesso in ShadowApplication:

ShadowApplication.runBackgroundTasks();

Problemi correlati