簡體   English   中英

函數expect_that從testthat遇到錯誤

[英]function expect_that from testthat runs into error

任何人都可以幫助我解釋為什么expect_that如果[]被添加到停止消息時不起作用,即f1有效,但f2沒有。

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(f2(x=1.4), throws_error("error \\[1\\]"))

似乎工作。

或者您可以指定fixed=TRUE

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM