Hello there!
I started this topic to gather opinions about the following issue.
It is considered a good practice (for readability and maintainability of package) to decompose big functions into application of smaller ones. For example:
do_action <- function(x, y) {
z <- do_first(x)
do_second(y, z)
}
It is common sense to test functions do_first and do_second separately from do_action. Those tests define desired behaviour for corresponding functions. However, the initial goal is define and test behaviour of do_action, which should lead to duplicating tests. As any sort of duplication is to be avoided in programming, my question is what is a good practice of resolving this test duplicating issue?
In testthat I found function describe which is used for "documenting ... intended behaviour". It can partially answer the question: use describe for big functions "to verify that you implement the right things" and test_that for smaller ones "to ensure you do the things right". However, I don't see much usage of this approach.
Personally I feel that defining new expectation expect_calls(expr, fun_names) might be a good solution. It will check that functions with names fun_names are called during evaluation of expression expr. So testing do_action will now be something like this:
expect_calls(
quote(do_action(1, 2)),
c("do_first", "do_second")
)
Any thoughts about this issue are welcome.