简体   繁体   English

如何在功能模块中对 Android 资源进行单元测试?

[英]How can I unit test Android resources in feature modules?

My app is using feature modules.我的应用程序正在使用功能模块。 One of the feature modules declares a bunch of strings in an xml resource, and then accesses them from my kotlin code: eg getString(R.string.dialog_title) .其中一个功能模块在 xml 资源中声明了一堆字符串,然后从我的 kotlin 代码中访问它们:例如getString(R.string.dialog_title) I want to run Junit tests to prove that the correct string content is retrieved.我想运行 Junit 测试来证明检索到了正确的字符串内容。

The Robolectric documentation page suggests using the function getApplicationContext<MyApplication>() . Robolectric 文档页面建议使用函数getApplicationContext<MyApplication>() However, you don't have access to the Application when unit testing a feature module.但是,在对功能模块进行单元测试时,您无权访问该应用程序。 I guess that's because feature modules exist separately to the application?我猜这是因为功能模块与应用程序分开存在?

Any help appreciated.任何帮助表示赞赏。 :) :)

Robolectric will slow your unit tests a bit. Robolectric 会稍微减慢您的单元测试速度。 You can just mock context object and then do something like: when(context.getString(any()).thenReturn('any/empty string')您可以模拟上下文对象,然后执行以下操作: when(context.getString(any()).thenReturn('any/empty string')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM