# NOT RUN {
f <- function() stop("My error!")
expect_error(f())
expect_error(f(), "My error!")
# You can use the arguments of grepl to control the matching
expect_error(f(), "my error!", ignore.case = TRUE)
# If you are working with classed conditions, it's better to test for
# the class name, rather than the error message (which may change over time)
custom_err <- function(var) {
rlang::abort("A special error", var = var, .subclass = "testthat_special")
}
expect_error(custom_err("a"), class = "testthat_special")
# Note that `expect_error()` returns the error object so you can test
# its components if needed
err <- expect_error(custom_err("a"), class = "testthat_special")
expect_equal(err$var, "a")
# }
Run the code above in your browser using DataLab