Answer the question
In order to leave comments, you need to log in
How to test complex functions with side effects?
Suppose, in accordance with the principle of separation of concerns, I have identified many small functions that solve their tasks, and covered them with tests. But after all there is also a certain general function which causes these small.
How to test this general function? After all, it covers all possible options for executing code in these small functions.
Do not test it at all, since all the functionality is already tested in small functions? This is not possible - what if I wrote this large function incorrectly (I called the small ones in the wrong order or forgot something). Tests are needed, the only question is which ones.
Also interested in how to test functions with side effects. Yes, I know about dependency injection, but it turns out that it must be used always, always, so that testing is possible? Will this complicate the code, make it more confusing and unreadable?
* * *
Well, to make it clearer, I will give a specific example. Suppose we need to process command line arguments passed to a program. Namely:
-v
print version if given ;function ProcessArgs(args) {
let parsed_args = ParseArgs(args)
ShowVersionIfNeed(parsed_args)
ValidateValues(parsed_args)
return parsed_args
}
ParseArgs()
, ShowVersionIfNeed()
and ValidateValues()
are also implemented and, what is important, covered with tests. ProcessArgs()
? Iterate over all possible options for all internal functions? That is, in fact, to sort through all the tests written for them in all possible combinations? Or how? ShowVersionIfNeed()
:function ShowVersionIfNeed(args) {
if args.contains('-v') {
sys.Exit(sys.ExitSuccess)
}
}
function ProcessArgs(args, exiter) {
let parsed_args = ParseArgs(args)
ShowVersionIfNeed(parsed_args, exiter)
ValidateValues(parsed_args)
return parsed_args
}
function ShowVersionIfNeed(args, exiter) {
if args.contains('-v') {
exiter.Exit(sys.ExitSuccess)
}
}
Answer the question
In order to leave comments, you need to log in
How to test this general function? After all, it covers all possible options for executing code in these small functions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question