rxjava 3
当我的RX链子应该引发例外但面临问题时,我正在尝试测试情况。当我尝试调用asserterror(error)
时,它说找不到错误。这里的示例:
fun isAvailable(): Single<String> {
return mapper.getSymbol().map { symbol ->
if (repository.isAvailable(symbol)) {
symbol
} else {
throw Exception("Symbol is not available")
}
}
}
在测试中,我模拟存储库返回false,然后做类似的事情:
val error = Exception("Symbol is not available")
whenever(mapper.getSymbol()).thenReturn(
Single.just(
symbol
)
)
whenever(repository.isAvailable(symbol)).thenReturn(false)
val test = symbolsRepository.isAvailable().test()
test.await()
.assertError(error)
但是当我运行测试时,我会看到 错误不存在(闩锁= 0,值= 0,错误= 1,完成= 0) 引起:java.lang.exception:符号不可用
I'm trying to test case when my rx chain should throw an exception but facing problem with it. When I'm trying to call assertError(error)
it says that no error found. Here's example:
fun isAvailable(): Single<String> {
return mapper.getSymbol().map { symbol ->
if (repository.isAvailable(symbol)) {
symbol
} else {
throw Exception("Symbol is not available")
}
}
}
In test I mock repository to return false and after do something like:
val error = Exception("Symbol is not available")
whenever(mapper.getSymbol()).thenReturn(
Single.just(
symbol
)
)
whenever(repository.isAvailable(symbol)).thenReturn(false)
val test = symbolsRepository.isAvailable().test()
test.await()
.assertError(error)
But when I run test I see
Error not present (latch = 0, values = 0, errors = 1, completions = 0)
Caused by: java.lang.Exception: Symbol is not available
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如@gpunto所说,
asserterror
确实等于exception
,默认情况下没有实现标准Java中的参考平等。您可以使用另外两个
asserterror
的过载来检查异常类及其消息:As @gpunto said,
assertError
does equals check on theException
and those are not implemented by default beyond reference equality in standard Java.You could use two other overloads of
assertError
to check for both the exception class and its message: