无法让我的断言当前活动正常工作

发布于 2024-12-11 08:21:19 字数 1516 浏览 0 评论 0原文

我有两个活动,第一个是启动活动,它的创建方法总是导致第二个活动启动。 我的 Robolectric 测试通过了良好的

活动

public class LoginActivity extends Activity {

/** Called when the activity is first created. */
@Override    

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loginview);

    Intent intent = new Intent(this,MainActivity.class);
    startActivity( intent );
}

我知道我的活动工作正常,因为它在我的设备和模拟器上启动

我的 Robolectric 测试

public void testLoginFirstTime() throws Exception 
{
    LoginActivity activity = new LoginActivity();        
    activity.onCreate(null);
    assertThat(activity, new StartedMatcher(MainActivity.class));        
}

我的 Robotium 测试

public void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
}


public void testLoginFirstTime() throws Exception 
{
    solo.assertCurrentActivity("Expected MainActivity to launch", MainActivity.class);
}

我的 Robotium 断言有什么问题?它总是认为当前活动是登录活动,即使当我观看模拟器/设备时,我可以看到 Robotium 确实启动了 MainActivity,但它似乎并不知道新活动已启动。 编辑:意思是说,如果我向登录视图添加一个按钮并通过单击按钮启动新活动,那么 Robotium 会执行单击并检测到新活动已启动。

编辑: 看起来这是 Robotium 的限制 http://groups.google.com/group/robotium-developers/ browser_thread/thread/79a70038c16e35e6 然而,它仍然给我留下了一个问题,即如何使用 Robotium 测试我的应用程序,就像用户使用它一样,即不作弊并开始不同的活动:(

I have 2 activities, the first one is the startup one which in it's create method causes the second one to be launched, always.
My Robolectric tests pass fine

Activity

public class LoginActivity extends Activity {

/** Called when the activity is first created. */
@Override    

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loginview);

    Intent intent = new Intent(this,MainActivity.class);
    startActivity( intent );
}

I know my activity works fine cause it launches in my device and on the emulator

My Robolectric tests

public void testLoginFirstTime() throws Exception 
{
    LoginActivity activity = new LoginActivity();        
    activity.onCreate(null);
    assertThat(activity, new StartedMatcher(MainActivity.class));        
}

My Robotium test

public void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
}


public void testLoginFirstTime() throws Exception 
{
    solo.assertCurrentActivity("Expected MainActivity to launch", MainActivity.class);
}

What is wrong with my robotium assertion? it always thinks the current activity is login one even though as I watch the emulator/device I can see that Robotium does actually launch the MainActivity but it doesn't appear to know that the new activity has been launched.
Edit: Meant to say if I add a button to my login view and launch the new activty via a button click then Robotium performs the click and detects the new activity has been launched ok.

Edit:
Looks like its a Robotium limitation
http://groups.google.com/group/robotium-developers/browse_thread/thread/79a70038c16e35e6
However it still leaves me with the issue of how to test my app with robotium the same way as a user will use it, ie, not cheating and starting in a different activity :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2024-12-18 08:21:19

您需要使用构造函数solo = new Solo(Instrumentation Instrumentation),然后在创建Solo对象后调用getActivity()以启动第一个Activity。然后它就会起作用。

You need to use the constructor solo = new Solo(Instrumentation instrumentation) and then after you have created the Solo object you call getActivity() in order to start the first Activity. Then it will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文