I have a couple functions that I'm trying to include in a package that ask for user input using scan(). I figured out how to unit test a function using testthat package for just one user input, by using ZNK's answer. I want to try to expand this to functions with multiple user inputs but I'm stuck on how to create multiple connections and reference them inside a function.
Here's a reproduced example:
# Simulate households with children
subpop_children <- function(df, n = 5){
# user input
cat("please enter a number here")
num1 <- scan(n = 1, what = double())
cat("Please enter three numbers separated by space:")
num2 <- scan(n = 3, what = double())
cat("Please enter three more numbers separated by space:")
num3 <- scan(n = 3, what = double())
cat("please enter a number")
num4 <- scan(n = 1, what = double())
return(rep(num2 + num3, num1+num4))
}
How can I unit test the above function using testthat library, if possible at all?
Would it be better to avoid user inputs and simply turn them into arguments to the function? Thank you in advance.