2012-05-30 41 views
1

我試圖推動我的測試在活動間通信中,並檢查例如正確登錄後,我產生了正確的活動(來自2個可能的活動)。robolectric測試中的鏈接活動

這裏是我的代碼如下所示:

@RunWith(GuiceRobolectricJUnitRunner.class) 
public class LoginActivityTest { 
@Inject 
private LoginActivity activity; 
@Inject 
private ExplorerActivity startedActivity ; 
@Inject 
private Context context; 

private Button loginButton; 
private EditText login; 
private EditText password; 

@Before 
public void setUp() throws Exception { 
    activity.onCreate(null); 
    loginButton = (Button) activity.findViewById(R.id.identification_login_button); 
    login = (EditText) activity.findViewById(R.id.txtLogin); 
    password = (EditText) activity.findViewById(R.id.txtPassword); 

} 

@Online 
@Test 
public void shouldExploreWhenLoginIsCorrect() throws Exception { 
    assertNotNull(activity); 
    login.setText("[email protected]"); 
    password.setText("test"); 
    activity.setIntent(new Intent()); 
    loginButton.performClick(); 
    ShadowActivity shadowActivity = Robolectric.shadowOf(activity); 
    Intent startedIntent = shadowActivity.getNextStartedActivity(); 
    ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent); 
    assertEquals(shadowIntent.getIntentClass(), ExplorerActivity.class); 
//  startedActivity.setIntent(startedIntent); 
//  startedActivity.onCreate(null); 


    } 
} 

我的問題是,我不能檢索從shadowintent的啓動活動。有什麼辦法可以達到這樣的效果嗎?此外,我沒有看到我的探索活動的任何跡象,我想知道Robolectric是否正在爲每個產卵過程進行沙箱工作。我真的很喜歡robolectric中鏈式活動測試的例子。謝謝。

回答

4

從3個月前開始,您可能已經找到了答案,如果沒有,您可以使用newInstance()來處理您已有的答案,然後按正常方式繼續。

ExplorerActivity explorerActivity = (ExplorerActivity) shadowIntent.getIntentClass().newInstance(); 
explorerActivity.setIntent(startedIntent); 
explorerActivity.onCreate(null); 
相關問題