Robolectric 自定义阴影对象
OOTB,Robolectric 不太支持区域设置。因此,如果您的应用程序依赖于语言环境(很多应用程序都是国际化的,如果它们正确的话),这可能是一个巨大的痛苦。长话短说,我创建了自己的 ShadowFooGeocoder 和 ShadowFooAddress,它们允许我模拟我想要的区域设置。它们基本上是现有阴影的重新实现。
但是,当我这样绑定我的类时:bindShadowClass(ShadowFooGeocoder.class),这效果很好。在运行时,返回正确的阴影。问题是我想在这个对象上设置模拟,但我不知道如何设置。 ShadowOf(instance)(其中instance是注入的GeoCoder)返回ShadowGeoCoder。我尝试直接使用 ShadowWrangler,但这也会返回 ShadowGeocoder。
如何获取通过bindShadowClass(...) 调用绑定的影子类,以便我可以设置我的期望(模拟)?
注意:这是 Robolectric 群组此处上同一问题的转发。我在这里发帖是因为我让任何人在小组中回答问题的成功率相当低。我希望在这里能有更好的结果。
OOTB, Robolectric does not support Locales that well. Therefore, if your app is dependent on locales (which a lot of apps are if they are i18n'nd properly) this can be a royal pain. Long story short, I created my own ShadowFooGeocoder and ShadowFooAddress that allow me to simulate the locale I want. They're basically re-implementations of the existing shadows.
However, when I bind my class as such: bindShadowClass(ShadowFooGeocoder.class), this works great. At runtime, the correct shadow is returned. The problem is that I want to set up the simulations on this object and I'm not sure how. shadowOf(instance) where instance is an injected GeoCoder returns ShadowGeoCoder. I've tried working directly with the ShadowWrangler, but that also returns a ShadowGeocoder.
How can I get at my shadowed class that I've bound through the bindShadowClass(...) call so I can set my expectations (simulations)?
Note: This is a repost of the same question on the Robolectric group here. I posted here because my success rate of getting anyone to answer questions on the group is fairly low. I'm hoping for a better result here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里基本上所做的是像这样扩展 ShadowGeocoder:
然后我将使用 bindShadowClasss(...) 绑定它,当我通过静态 ShadowOf(...) 调用检索阴影时,我会得到一个“ShadowGeocoder”,其中是 ShadowFooBarGeocoder 的一个实例。然后我将其转换为该类型并执行我需要的任何工作。
What I've basically done here is extend ShadowGeocoder like this:
Then I would bind it using the bindShadowClasss(...) and when I retreive the shadow via the static shadowOf(...) call I get back a "ShadowGeocoder" which is an instance of ShadowFooBarGeocoder. I then cast it to that type and perform whatever work I need to.