无法让我的断言当前活动工作 [英] Can't get my assert current activity to work

查看:36
本文介绍了无法让我的断言当前活动工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个活动,第一个是启动活动,它的 create 方法导致第二个活动始终启动.我的 Robolectric 测试通过了

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

活动

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

我的 Robolectric 测试

My Robolectric tests

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

我的 Robotium 测试

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);
}

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

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. 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.

看起来像是 Robotium 的限制http://groups.google.com/group/robotium-developers/浏览线程/线程/79a70038c16e35e6然而,它仍然给我留下了一个问题,即如何使用机器人测试我的应用程序与用户使用它的方式相同,即不作弊并从不同的活动开始:(

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 :(

推荐答案

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

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.

这篇关于无法让我的断言当前活动工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆