来自测试的函数Expect_that会出错 [英] function expect_that from testthat runs into error

查看:64
本文介绍了来自测试的函数Expect_that会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解释一下,如果在停止消息中添加[]expect_that不起作用的原因,即f1起作用但f2无效.

Can anybody help me and explain why expect_that doesn't work if [] is added to the stop message, i.e. f1 works but f2 doesn't.

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]"))

推荐答案

expect_that正在寻找正则表达式来匹配错误,因此您需要对方括号进行转义,以便它们完全按字面解释而不是作为模式定义:

expect_that is looking for a regular expression to match the error, so you need to escape the square-brackets so that they are interpreted literally rather than as a pattern definition:

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

似乎可以正常工作.

或者您可以指定fixed=TRUE:

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

这篇关于来自测试的函数Expect_that会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆