你如何强制在Android Robolectric测试配置变化? [英] How do you force a configuration change in an Android Robolectric test?

查看:128
本文介绍了你如何强制在Android Robolectric测试配置变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用robolectric,使我的机器人单元测试速度不够快是有益的。我想测试code我写的作品,同时屏幕方向正在发生变化,以模拟一个共同的现实世界中的用例。

I'm using robolectric to make my android unit tests fast enough to be useful. I want to test that code I've written works while the screen orientation is changing to simulate a common real world use case.

具体是什么,我的测试是一个异步HTTP调用与一些XML解析后的结果是牵强的服务器。我有单元​​测试为所有伟大的工作,但无法弄清楚如何模拟屏幕旋转。任何状态变化,导致该活动重新创建本身是好的,它并不一定必须是屏幕旋转。

Specifically what I'm testing is an asynchronous http call to a server with some xml parsed after the result is fetched. I have the unit test for all that working great but can't figure out how to simulate the screen rotation. Any state change that causes the activity to recreate itself is fine, it doesn't necessarily have to be screen rotation.

这是使用仿真器解决方案是不是一种选择,因为我跑我的测试中,每分钟几次,他们必须在2秒运行。我也想这与roboguice如果可能的工作。

A solution that uses the emulator is not an option as I run my tests several times per minute and they must run under 2 seconds. I would also like this to work with roboguice if possible.

感谢。

推荐答案

什么Android的API级别在编译反对?如果是3.0或以上,你可以尝试 Activity.recreate() 。该文件规定:

What Android API level are you compiling against? If it's 3.0 or above you could try Activity.recreate(). The documentation states:

因为这活动与一个新的实例被创建。这将导致基本上相同的流时,活性归因于配置改变创建为 - 当前实例将通过它的生命周期的onDestroy(),然后后它创建了一个新的实例

Cause this Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it.

还没有尝试过自己,但。

Haven't tried it myself though.

这篇关于你如何强制在Android Robolectric测试配置变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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