f <- function(x) {
if (x < 0) message("*x* is already negative")
-x
}
expect_that(f(-1), shows_message())
expect_that(f(-1), shows_message("already negative"))
expect_that(f(1), shows_message())
expect_message(f(-1))
expect_message(f(-1), "already negative")
expect_message(f(1))
# You can use the arguments of grepl to control the matching
expect_message(f(-1), "*x*", fixed = TRUE)
expect_message(f(-1), "NEGATIVE", ignore.case = TRUE)
Run the code above in your browser using DataLab