[英]Parentheses in error message cause expect_error test to fail
为什么这个测试没有通过?
my_fun <- function(x){
if(x > 1){stop("my_fun() must be called on values of x less than or equal to 1")}
x
}
library(testthat)
expect_error(my_fun(2),
"my_fun() must be called on values of x less than or equal to 1")
它返回错误消息:
错误:错误$消息不匹配“必须在x小于或等于1的值上调用my_fun()”。 实际值:“必须在x小于或等于1的值上调用my_fun()”
如果您同时从函数和测试中删除()
,则测试会通过,导致我认为这与括号有关。
在expect_error
,您传递的是正则表达式,而不仅仅是字符串。 括号是正则表达式中的特殊字符,必须转义。 (括号用于分组正则表达式)。 要处理parens,只需将expect_error
更改为以下内容:
expect_error(my_fun(2),
"my_fun\\(\\) must be called on values of x less than or equal to 1")
或更笼统地说,指定fixed = TRUE
以测试字符串是否完全匹配:
expect_error(my_fun(2),
"my_fun() must be called on values of x less than or equal to 1",
fixed = TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.