I was curious how someone would approach unit testing the following pseudo coded function or even refactor to make it easier to test the different pieces.
To begin, we have a large code base that, at a high level, is broken down into the following projects:
Orchestrations -> Services -> Repositories -> Database
-> Behaviors
The current example I'm working with is at the orchestration level there is a function as follows:
FUNCTION Process (Options)
IF Options.Option1 THEN
IF Service1.HasAnyItems THEN
Service1.DoSomethingWithThoseItems
FI
FI
IF Options.Option2 THEN
IF Service2.HasAnyItems THEN
Service2.DoSomethingWithThoseItems
FI
FI
IF Options.Option1 OR Options.Option2 THEN
Orchestration2.DoSomething
FI
END FUNCTION
I immediately see 4 different test scenarios that will produce a different output:
- Option 1 is true, Option 2 is false
- Option 2 is true, Option 1 is false
- Option 1 is true, Option 2 is true
- Option 1 is false, Option 2 is false
Currently the function doesn't return anything because the services and orchestration that are called to a variety of things (that are tested separately). To add further challenges, the result of the orchestration call can produce different side effects based on settings that it will internally fetch.
Previously, I have accomplished testing a function like this by mocking out the services and orchestrations and asserting the function was "called". However, I'm not a big fan of this as the mocks are tedious and the tests are very fragile because internal function changes will easily break the tests.