Testthat's expect_that function is failing

Can someone help me and explain why expect_that does not work if [] added to the stop message, i.e. f1 works, but f2 does not work.

 library(testthat) f1 <- function(x){ if( x >= 1 ){ stop("error 1") } } expect_that(f1(x=1.4), throws_error("error 1")) f2 <- function(x){ if( x >= 1 ){ stop("error [1]") } } expect_that(f2(x=1.4), throws_error("error [1]")) 
+4
r testthat
source share
1 answer

expect_that looks for a regular expression that matches the error, so you need to avoid the square brackets so that they are interpreted literally, and not as a template definition:

 expect_that(f2(x=1.4), throws_error("error \\[1\\]")) 

seems to work.

Or you can specify fixed=TRUE :

 expect_that(f2(x=1.4), throws_error("error [1]", fixed = TRUE)) 
+7
source share

All Articles