AndroidViewModel 和单元测试
AndroidViewModel and Unit Tests
我正在使用 AndroidViewModel
和 LiveData
将 Intent 发送到 IntentService
并从 EventBus 接收事件。我需要 Intents 和 EventBus 的应用程序上下文。
使用本地测试测试 AndroidViewModel 类 的最佳方法是什么?我可以从 Robolectrics RuntimeEnvironment.application 开始,但似乎没有 AndroidViewModel 的 shadowOf() 来检查是否将正确的 Intents 发送到正确的接收器。
也许可以通过 Mockito 使用我自己的模拟意图将它们注入到我的 AndroidViewModel
中以某种方式做到这一点,但这似乎不是很简单。
我的代码看起来像这样:
class UserViewModel(private val app: Application) : AndroidViewModel(app){
val user = MutableLiveData<String>()
...
private fun startGetUserService() {
val intent = Intent(app, MyIntentService::class.java)
intent.putExtra(...)
app.startService(intent)
}
@Subscribe
fun handleSuccess(event: UserCallback.Success) {
user.value = event.user
}
}
Robolectric 测试:
@RunWith(RobolectricTestRunner.class)
public class Test {
@Test
public void testUser() {
UserViewModel model = new UserViewModel(RuntimeEnvironment.application)
// how do I test that startGetUserService() is sending
// the Intent to MyIntentService and check the extras?
}
我宁愿为您的 Application
class 创建一个模拟,因为这样它就可以用来验证调用了哪些方法以及将哪些对象传递给了这些方法。所以,它可能是这样的(在 Kotlin 中):
@RunWith(RobolectricTestRunner::class)
class Test {
@Test
public void testUser() {
val applicationMock = Mockito.mock(Application::class.java)
val model = new UserViewModel(applicationMock)
model.somePublicMethod();
// this will capture your intent object
val intentCaptor = ArgumentCaptor.forClass(Intent::class.java)
// verify startService is called and capture the argument
Mockito.verify(applicationMock, times(1)).startService(intentCaptor.capture())
// extract the argument value
val intent = intentCaptor.value
Assert.assertEquals(<your expected string>, intent.getStringExtra(<your key>))
}
}
我正在使用 AndroidViewModel
和 LiveData
将 Intent 发送到 IntentService
并从 EventBus 接收事件。我需要 Intents 和 EventBus 的应用程序上下文。
使用本地测试测试 AndroidViewModel 类 的最佳方法是什么?我可以从 Robolectrics RuntimeEnvironment.application 开始,但似乎没有 AndroidViewModel 的 shadowOf() 来检查是否将正确的 Intents 发送到正确的接收器。
也许可以通过 Mockito 使用我自己的模拟意图将它们注入到我的 AndroidViewModel
中以某种方式做到这一点,但这似乎不是很简单。
我的代码看起来像这样:
class UserViewModel(private val app: Application) : AndroidViewModel(app){
val user = MutableLiveData<String>()
...
private fun startGetUserService() {
val intent = Intent(app, MyIntentService::class.java)
intent.putExtra(...)
app.startService(intent)
}
@Subscribe
fun handleSuccess(event: UserCallback.Success) {
user.value = event.user
}
}
Robolectric 测试:
@RunWith(RobolectricTestRunner.class)
public class Test {
@Test
public void testUser() {
UserViewModel model = new UserViewModel(RuntimeEnvironment.application)
// how do I test that startGetUserService() is sending
// the Intent to MyIntentService and check the extras?
}
我宁愿为您的 Application
class 创建一个模拟,因为这样它就可以用来验证调用了哪些方法以及将哪些对象传递给了这些方法。所以,它可能是这样的(在 Kotlin 中):
@RunWith(RobolectricTestRunner::class)
class Test {
@Test
public void testUser() {
val applicationMock = Mockito.mock(Application::class.java)
val model = new UserViewModel(applicationMock)
model.somePublicMethod();
// this will capture your intent object
val intentCaptor = ArgumentCaptor.forClass(Intent::class.java)
// verify startService is called and capture the argument
Mockito.verify(applicationMock, times(1)).startService(intentCaptor.capture())
// extract the argument value
val intent = intentCaptor.value
Assert.assertEquals(<your expected string>, intent.getStringExtra(<your key>))
}
}