【发布时间】:2019-05-25 12:09:37
【问题描述】:
Android Studio 3.4
我正在测试以下方法。基本上,这个测试所做的是发出一个请求,这将返回一个LoginResponseEntity,该LoginResponseEntity将被映射并返回一个Single<LoginResponse>
override fun loginUserPost(username: String, password: String, uniqueIdentifier: String, deviceToken: String, apiToken: String) : Single<LoginResponse> {
val loginRequestEntity = LoginRequestEntity(username, password, uniqueIdentifier, deviceToken)
return loginAPIService.loginUserPost(loginRequestEntity, apiToken)
.map {
loginResponseDomainMapper.map(it)
}
}
我写的测试用例有效,但我认为这并没有完全测试这种方法。
@Test
fun `should return LoginResponse`() {
val loginRequestEntity = LoginRequestEntity("username", "password", "uniqueidentifier", "devicetoken")
val loginResponse = LoginResponse("token", createUser(), emptyList(), emptyList())
val loginResponseEntity = LoginResponseEntity("token", createUserEntity(), emptyList(), emptyList())
whenever(loginAPIService.loginUserPost(loginRequestEntity, "apitoken")).thenReturn(Single.just(loginResponseEntity))
loginServiceImp.loginUserPost("username", "password", "uniqueidentifier", "devicetoken", "apitoken")
.test()
.assertValue(loginResponse)
verify(loginAPIService).loginUserPost(loginRequestEntity, "apitoken")
}
private fun createUser() =
User(
"id",
"email",
"firstname",
"lastname",
"phone",
"address",
"dob",
"customer",
listOf("enterpriseids"),
listOf("vendorids"))
private fun createUserEntity() =
UserEntity(
"id",
"email",
"firstname",
"lastname",
"phone",
"address",
"dob",
"customer",
listOf("enterpriseids"),
listOf("vendorids"))
}
我还能做些什么来测试这个方法。我应该测试这个方法的.map{loginResponseDomainMapper.map(it) 部分吗?
【问题讨论】:
-
通常你首先测试的是函数的结果。您可以使用 TestScheduler 来做到这一点
-
除非您使用某种代码覆盖率工具,否则您永远不会知道您的代码是否经过全面测试,您可以尝试
jacoco甚至idea包含一个 -
你只是在测试快乐的道路。如果
loginAPIService.loginUserPost()方法失败了怎么办?