如何准确地定义阴影对象Robolectric工作? [英] How does exactly custom Shadow objects work in Robolectric?

查看:213
本文介绍了如何准确地定义阴影对象Robolectric工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写了一个自定义的影子为我的活动,并与RobolectricTestRunner注册它,会拦截与我的自定义阴影,只要它开始了活动的框架?

If I write a custom Shadow for my Activity, and registering it with RobolectricTestRunner, will the framework intercept the Activity with my custom Shadow whenever it's started?

感谢。

推荐答案

简短的答案是否定的。

Robolectric是有选择性的什么类它拦截和手段。在写这篇文章的时候,将仪器唯一的类必须有一个完全合格的类名匹配这些选择之一:

Robolectric is selective about what classes it intercepts and instruments. At the time of this writing, the only classes that will be instrumented must have a fully qualified classname match one of these selectors:

android.* 
com.google.android.maps.* 
org.apache.http.impl.client.DefaultRequestDirector

的全部理由Robolectric的存在是,当在JVM中调用的(即不是在一个模拟器或设备)在Android SDK的jar提供的类抛出异常。您的应用程序的活动有来源,是不是敌对(它可能不会引发异常的方法或构造函数调用时)。 Robolectric的预期目的,是让你把你的应用程序的code下测试,否则将是不可能的,由于SDK的写入方法。一些为什么Robolectric创建另一个原因是:

The whole reason for Robolectric's existence is that the classes provided in the Android SDK jar throw exceptions when invoked in a JVM (i.e. not on an emulator or device). Your application's Activity has source that is not 'hostile' (it probably does not throw exceptions when the methods or constructors are invoked). Robolectric's intended purpose is to allow you to put your application's code under test, which would otherwise not be possible due to the way the SDK is written. Some of the other reasons why Robolectric was created were:

  • 在该SDK并不一定有方法,让您查询您的应用程序的code操纵机器人对象的状态。阴影可以被写入以提供进入该状态。
  • 很多在Android SDK中的类和方法都最终和/或私有或受保护的,因此很难创建应用程序所需的code的依赖关系,否则将提供给您的应用程序code。

在code可以清楚地改为影子任何类。有人谈到过去有关提取阴影功能集成到一个独立的库,以协助使用一些其他的测试敌对API编写测试。

The code could clearly be changed to shadow any class. There has been talk in the past about extracting the shadowing features into a standalone library, to assist writing tests using some other test-hostile api.

为什么要阴影你的活动?

Why do you want to shadow your Activity?

这篇关于如何准确地定义阴影对象Robolectric工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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