What must I do to create a unit test that causes searchResult to be null so that MyMethod returns false?
public class MyConnectionHandler
public bool MyMethod()
{
var myConnection = new LdapConnection(...);
var searchResult = myConnection.Search(...);
if (searchResult == null) return false;
return true;
}
}
I'm encountering these problems:
- I'm unable to mock
myConnectionbecause it's not injected as a dependency, and I prefer not to make it injectable because no other class thanMyConnectionHandlerwill be using it. - The Search function connects to an external server that I could configure to return null, but I prefer that the test depends on its own configuration rather than that of an external server.
I probably need to refactor the class, but it's unclear to me how to do that correctly and I much appreciate your help.
If this line of code
somehow reads some configuration information that you can intercept, and if you can stand up an in-memory LDAP server, like you can stand up an automatically configured database (SQL) server, you may be able to write an integration test that verifies the code path that you're interested in.
If that's not possible...
Yes, you do, and the easiest way to do that is to inject the dependency, even though you wrote that you don't want to do that.
That may not be the best criterion for deciding what gets injected and what doesn't. Rather, you should model those dependencies that are real dependencies as interfaces or base classes, and little more. This is because these are your inescapable sources of non-deterministic behaviour, and since you want tests to be deterministic, you want to be able to control those dependencies from your test.
Thus,
MyConnectionHandlershould look like this:Now you can create a Test Double and inject it into an instance of
MyConnectionHandler.