How to write a NUnit test for a method like this. Does this method itself warrant refactoring? What is the best approach to deal with scenarios like this in leagacy code?
public bool DoXYZ()
{
ABC abc= new ABC()
XYZ xyz = new XYZ();
if (xyz .IsSomeCondition(Session.SessionID))
{ return false; }
else
{ return abc.IsSomeOtherCondition(SessionID.SessionID); }
}
You will probably need to refactor it to introduce hooks for dependency injection. For example, the class that contains the DoXYZ method can get new properties for ABC and XYZ. These properties could default to instances of ABC and XYZ, but in the unit tests could be replaced by mock versions.
And if you prefer to use IoC, this approach supports that as well
I would definitely refactor to inject the session id via a parameter - otherwise you'll have to create the session manually.
Can it be made static? Looks like it, particularly if you inject sessionid.
Also, you're implementing a (short) command dispatcher, which is generally considered an anti-pattern, compared to IoC (see Joel Martinez' answer above).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With