我有以下功能:
fun = function(expr) {
mc = match.call()
env = as.environment(within(
list(),
expr = eval(mc$expr)
))
return(env)
}
在 a 中调用它,tryCatch()
以便expr
优雅地处理其中的任何错误条件。
它适用于标准错误条件:
tryCatch({
fun({
stop('error')
})
}, error = function(e) {
message('error happened')
})
# error happened
但是,它不会捕获testthat
预期错误(这对于我的特定用例来说是首选):
library(testthat)
tryCatch({
fun({
expect_true(FALSE)
})
}, error = function(e) {
message('expectation not met')
})
# Error: FALSE isn't true.
或更简单地说:
library(testthat)
tryCatch({
expect_true(FALSE)
}, error = function(e) {
message('expectation not met')
})
# Error: FALSE isn't true.
未捕获期望错误。
从 R 3.2.2 升级到 R 3.3.0 后出现此问题 - 即在 R 3.2.2 中发现了预期错误。
有没有办法让R 3.3.0 中testthat
的期望得到满足?tryCatch()