admin管理员组文章数量:1420159
I have a function which is being called in multiple functions. All of them sit in separate files. Let's take an example to understand it.
# R/generic_function.R
generic_function <- function() {
....
....
}
# R/functionA.R
function_A <- function() {
...
generic_function()
...
}
# R/functionB.R
function_B <- function() {
...
generic_function()
...
}
I am writing tests for them using testthat
in corresponding test files where I am mocking the generic_function()
# tests/testthat/test-functionA.R
mockery::stub(function_A, "generic_function", TRUE)
...
...
# tests/testthat/test-functionB.R
mockery::stub(function_B, "generic_function", TRUE)
...
....
There are too many such function*
files and corresponding test-function*
files. I am wondering if there is a way where I can mock such files on global level only once?
I have a function which is being called in multiple functions. All of them sit in separate files. Let's take an example to understand it.
# R/generic_function.R
generic_function <- function() {
....
....
}
# R/functionA.R
function_A <- function() {
...
generic_function()
...
}
# R/functionB.R
function_B <- function() {
...
generic_function()
...
}
I am writing tests for them using testthat
in corresponding test files where I am mocking the generic_function()
# tests/testthat/test-functionA.R
mockery::stub(function_A, "generic_function", TRUE)
...
...
# tests/testthat/test-functionB.R
mockery::stub(function_B, "generic_function", TRUE)
...
....
There are too many such function*
files and corresponding test-function*
files. I am wondering if there is a way where I can mock such files on global level only once?
1 Answer
Reset to default 0I solved it using setup.R
and teardown.R
files. These files are special and have a some special characteristics.
setup.R
file is ran before every test file. In setup.R
we replace generic_function
with mock_generic_function
in global environment.
# tests/testthat/setup.R
mock_generic_function <- function(...) {
TRUE
}
message("Running setup.R before the test...")
assign("generic_function ", mock_generic_function , envir = globalenv())
teardown.R
is ran after all the tests are run where we remove the (mocked) generic_function
from global environment.
rm(generic_function, envir = globalenv())
message("Running teardown.R after the tests have been executed...")
PS - From the help file, the usage of setup
and teardown
is superseded however, this approach works well for me so I am using this for time being.
本文标签: rHow can I mock a function globally using testthatStack Overflow
版权声明:本文标题:r - How can I mock a function globally using testthat? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745309267a2652838.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
A
callsB
andB
callsgeneric_function
, then mocking onlyA
should work, unless ‘mockery’ uses a questionable design). Of course if you are callingfunction_A
,function_B
, etc. individually from your tests, that won’t help you. — But you could write a helpercall_mocked()
and then use that inside your tests; i.e. instead of callingfunction_A(…)
, you’d usecall_mocked(function_A, …)
for example. – Konrad Rudolph Commented Jan 29 at 7:58testthat::local_mocked_bindings(generic_function = \(…) TRUE)
in "tests/testthat/helpers.R" or "tests/testthat/setup.R"? – zephryl Commented Jan 29 at 12:19