【发布时间】:2022-01-14 08:36:51
【问题描述】:
我编写了一个从 aws s3-bucket 获取单个文件的导入函数。
该函数本身是一个包装器 arround aws.s3::s3read_using(),它将读取函数作为其第一个参数。
我为什么要环绕 aws.s3::s3read_using() ?因为我需要做一些特殊的错误处理,并且希望包装函数能做一些Recall() 达到极限......但那是另一回事了。
现在我已经成功构建并测试了我的包装功能,我想再做一个包装:
我想在我的包装器上迭代 n 次以将下载的文件绑定在一起。我现在很难将“reading_function”交给aws.s3::s3read_using() 的FUN 参数。
我可以通过简单地使用 ... 来做到这一点 - 但是!
我想向包装器的 USER 说明,他需要指定该参数。
所以我决定使用 rlangs rlang::enexpr() 来捕获参数并通过 !! 将其移交给我的第一个包装器 - 作为回报,它会再次使用 rlang::enexpr() 捕获该参数并将其移交 - 最后 -到aws.s3::s3read_using() 通过rlang::expr(aws.s3::s3read_using(FUN = !!reading_fn, object = s3_object))
效果非常好,非常流畅。我的问题是使用testthat 和mockery 测试该函数构造
下面是一些大体上简化的代码:
my_workhorse_function <- function(fn_to_work_with, value_to_work_on) {
fn <- rlang::enexpr(fn_to_work_with)
# Some other magic happens here - error handling, condition-checking, etc...
out <- eval(rlang::expr((!!fn)(value_to_work_on)))
}
my_iterating_function <- function(fn_to_iter_with, iterate_over) {
fn <- rlang::enexpr(fn_to_iter_with)
out <- list()
for(i in seq_along(iterate_over)) {
out[[i]] <- my_workhorse_function(!!fn, iterate_over[i])
}
return(out)
}
# Works just fine
my_iterating_function(sqrt, c(9:16))
现在开始测试:
# Throws an ERROR: 'Error in `!fn`: invalid argument type'
test_that("my_iterating_function iterates length(iterate_over) times over my_workhorse_function", {
mock_1 <- mockery::mock(1, cycle = TRUE)
stub(my_iterating_function, "my_workhorse_function", mock_1)
expect_equal(my_iterating_function(sqrt, c(9:16)), list(1,1,1,1,1,1,1,1))
expect_called(mock_1, 8)
})
我使用了一个workarround,但感觉不对,尽管它有效:
# Test passed
test_that("my_iterating_function iterates length(iterate_over) times over my_workhorse_function", {
mock_1 <- mockery::mock(1, cycle = TRUE)
stub(my_iterating_function, "my_workhorse_function",
function(fn_to_work_with, value_to_work_on) {
fn <- rlang::enexpr(fn_to_work_with)
out <- mock_1(fn, value_to_work_on)
out})
expect_equal(my_iterating_function(sqrt, c(9:16)), list(1,1,1,1,1,1,1,1))
expect_called(mock_1, 8)
})
我正在使用R: 4.1.1 的版本
我正在使用testthat(3.1.1)、mockery(0.4.2)、rlang(0.4.12) 的版本
【问题讨论】:
标签: r unit-testing mockery testthat quasiquotes