testthat
包中的expect_error()
的正確用法是什麼?我試圖從幫助中調整示例,但是在錯誤消息中使用括號時會失敗。如何正確使用testthat expect_error()?
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(")
這導致:
> 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 ')''
ř版本3.0.1(2013年5月16日)
的可能的複製[功能預計\ _that從testthat運行到錯誤](http://stackoverflow.com/questions/28266954/function-expect-that-from- testthat-runs-into-error) –