Qual è l'uso corretto di expect_error()
nel pacchetto testthat
? Ho provato ad adattare l'esempio di aiuto, ma questo fallisce quando uso parentesi nel messaggio di errore.Come utilizzare testthat expect_error() correttamente?
library(testthat)
# Works
tmp1 <- function() stop("Input is not correct")
expect_error(tmp1(),"Input is not correct")
# Does not work
tmp2 <- function() stop("Input (x) is not correct")
expect_error(tmp2(),"Input (x) is not correct")
# Does not work
tmp3 <- function() stop("Input(")
expect_error(tmp3(),"Input(")
Ciò comporta:
> library(testthat)
>
> # Works
> tmp1 <- function() stop("Input is not correct")
> expect_error(tmp1(),"Input is not correct")
> # Does not work
> tmp2 <- function() stop("Input (x) is not correct")
> expect_error(tmp2(),"Input (x) is not correct")
Error: tmp2() does not match 'Input (x) is not correct'. Actual value:
Error in tmp2() : Input (x) is not correct
> # Does not work
> tmp3 <- function() stop("Input(")
> expect_error(tmp3(),"Input(")
Error in grepl("Input(", "Error in tmp3() : Input(\n", fixed = FALSE, :
invalid regular expression 'Input(', reason 'Missing ')''
R versione 3.0.1 (2013-05-16)
Possibile duplicato del [funzione si aspettano \ _that da testthat corre in errore] (http://stackoverflow.com/questions/28266954/function-expect-that-from- testthat-runs-into-error) –