【发布时间】:2021-05-08 17:41:07
【问题描述】:
我是 {testthat} 的新手,我正在为一个修改字符串的函数构建测试,并希望为某些输入模式生成特定的输出。
作为示例(下面的表示形式),add_excitement 在其输入字符串中添加了一个感叹号。当输入“hello”时,它应该返回“hello!”;当给出任何其他输入时,它应该不返回“hello!”。我想{testthat}一系列模式的行为并返回信息性错误,指定哪个模式导致错误。
根据 {testthat} 包文档,我相信我应该使用expect_match。但是,这会引发“无效参数类型”错误,而 expect_identical 有效。我不明白为什么会这样。我的问题是:
- 为什么
expect_identical而不是expect_match接受quasi_label参数? - 我是否可以使用
expect_identical而不是expect_match来实现我的目的,或者这会带来其他错误的风险吗?
这是一个代表:
library(testthat)
library(purrr)
patterns = c("hello", "goodbye", "cheers")
add_excitement <- function(pattern) paste0(pattern, "!")
# For a single pattern
show_failure(expect_identical(add_excitement(!!patterns[2]), "hello!"))
#> Failed expectation:
#> add_excitement("goodbye") not identical to "hello!".
#> 1/1 mismatches
#> x[1]: "goodbye!"
#> y[1]: "hello!"
try(
show_failure(expect_match(add_excitement(!!patterns[2]), "hello!", fixed = TRUE,all = TRUE))
)
#> Error in !patterns[2] : invalid argument type
# For multiple patterns
purrr::map(
patterns,
~ show_failure(expect_identical(add_excitement(!!.), "hello!"))
)
#> Failed expectation:
#> add_excitement("goodbye") not identical to "hello!".
#> 1/1 mismatches
#> x[1]: "goodbye!"
#> y[1]: "hello!"
#> Failed expectation:
#> add_excitement("cheers") not identical to "hello!".
#> 1/1 mismatches
#> x[1]: "cheers!"
#> y[1]: "hello!"
#> [[1]]
#> NULL
#>
#> [[2]]
#> NULL
#>
#> [[3]]
#> NULL
try(
purrr::map(
patterns,
~ show_failure(expect_match(add_excitement(!!.), "hello!",
fixed = TRUE, all = TRUE)
)
)
)
#> Error in !. : invalid argument type
由reprex package (v0.3.0) 于 2021-02-04 创建
感谢您的帮助!
【问题讨论】:
标签: r rlang testthat quasiquotes