I’m fairly new to unit testing but I completely get the idea of testing individual units of code that perform a specific, testable task, however, I’m in a position where I need to write tests and provide confidence in the accuracy of an output of a method that acts on an object with over 50 properties. The combinations of the values of these properties produce an output based on rules injected from a rule definition object (using lambda expressions) which essentially equates to a percentage. These output percentages are “mission critical” and have been rather lazily tested previously, for example the quality of the rule definition class (do all the attributable percentages for each rule add up to 100%) but the actual properties of the object haven’t been.
The “data” object comes from a database but I can, of course, mock it. My problem is the number of permutations of data that would need mocking and the amount of tests that would need to be written to ensure that data x,y,z (times 50 odd exponential) feels near impossible.
So, the question is, how are these situations testable in a real sense. Is scripting tests based on a known “correct” state and “correct” results even possible/sensible? Are unit tests applicable in this case and if not what alternatives are there.
By the way, this is legacy code here with a small opportunity to refactor but only if I can guarantee accuracy etc within timescales of a couple of days to do both the refactor and the tests!
I think you’ve given half of your answer yourself:
In your current unit test, you’d be mocking both the data and the rule. Therefore, you’d need only to ensure that the in- and output methods behave correctly.
Testing the rule is a different task. I can only guess, but usually, you’ll have an Excel table, or something like that, of possible in- and output values to specify the requirements for this rule. I would convert that same table to a readable (csv) format, and use it directly to drive the rule’s unit test.