The factory static class introduces a relationship between your class and the HeightBounds
class. this can make your class difficult to test if, for example, HeightBounds
disconnects and looks for information in the database, or reads from a web service, etc. etc.
If instead you IHeightBounds
implementation into your class, then you can mock it so you can check what happens when your class dependencies do certain things.
For example, what if HeightBounds
throws an exception? or returns null? Or do you want to check when a specific HeightBound
? With an interface, it’s easy to mock this behavior with a static factory, it's harder because you produce data to create the desired results in the class.
You can still only have one HeightBounds
implementation, and you can test it separately, but you can test your method above without even having a real implementation.
I would probably have an IHeightBoundFactory
interface and IHeightBoundFactory
implementation into the class.
As for testing privates, as a rule, you do not want this. You want to experience one of two things, either that the results are what you expected, or that interactions are what you expected.
If you have a method called Add
and a method called GetAll
, then you can test it when you call Add
and then call GetAll
, you return the one you added. you don’t care how it is implemented, just the way it works. this is testing the results. Typically, in this situation, you want to create a mockup of objects that return data. This seems to be your situation.
If, when you call Add
you expect that what is being added is being registered, then you want to test the interaction with the registration dependency, so you add the dependency on mock and verify that the interaction with this class happened when you called Add
. Typically, in this situation, you want to create a mock of objects with established expectations and make sure that these expectations are met. It does not seem to be necessary in the situation described above.