The problem
Let's "draw" a picture of the situation:
- I have a SUT. (a good thing to have :P )
- I can inject some dependencies on my SUT.
- In a method I do a: new OtherClass(ParametersObtainedFromDependancies).
- I want to unit test that method in my SUT without handling the complexity of creating mock object that make sense for OtherClass to work correctly. This will imply to test OtherClass.
My solution to this was a factory method using a delegate:
public Func<OtherClass,Param1Type> GetOtherClass =
(param1) => new OtherClass(param1);
The bad: it's public. You could think on it as an optional dependency that you can override if need. But anyway, public smells.
The good: I don't need to create a MyTestSUT that will override this method, or even use a mock on the SUT to override that method.
Question
Is there any better solution? Is this correct?