I am trying to work out the best practice for my problem. I have multiple methods within a class that create new objects:
public void ToBeTested()
{
ClassForExmaple classForExample = new ClassForExample();
//Other logic.....
}
public void ToBeTested2()
{
ClassForExmaple2 classForExample2 = new ClassForExample2();
//Other logic.....
}
etc…..
I have a total of about 5 of these objects, which I was going to use constructor injection on but this seems quite messy. As there is a few of them should I be using private properties instead or is there another way?
I am doing this so I am able to mock these new calls out.
It seems fine to use constructor injection in this case, as it looks like you have a single instance of each type of object. If that isn’t the case, you could inject factories into the class’s constructor and allow it to create the objects as required.
I would avoid mocking private properties; if you’re having to resort to that, it usually means that you have hidden testable code inside your class that should be better exposed to the testing framework. Exposing public properties to be set externally would be better.