My approach to unit testing feels very wrong. I feel like I am missing something very basic, and I'm looking for some insight please.
Let's assume that I am testing a method (I'm using NUnit) that returns an object. It might look like this:
public void TestConnection()
{
DBConnector dbConn = new DBConnector(connString);
Assert.IsInstanceOf(typeof(DBConnector), dbConn);
}
Let's assume too that the test passes.
Later in my test class, I create another test on an object that has a dependency on MyObject
. That test method might look like this:
public void IsCallInstanceNull()
{
DBConnector dbConn = new DBConnector(connString);
dbConn.EstablishConnection();
CallInstance ci = new CallInstance(dbConn);
Assert.IsNotNull(ci);
}
I am first marking TestConnection() "public void", ensuring a basic pass, then changing the method to private so that I can use a typed object in the second method, instead of duplicating code. So my example test class becomes:
private DBConnector CreateDBConnector()
{
var dbConn = new DBConnector(connString);
dbConn.EstablishConnection();
return dbConn;
}
public void IsCallInstanceNull()
{
CallInstance ci = new CallInstance(CreateDBConnector());
Assert.IsNotNull(ci);
}
But this feels completely wrong. No one will ever know that the constructor for DBConnector
was ever tested (since not only did I change its visibility, I renamed the method!) so how can this be refactored so that I ensure test coverage for the private method without having to leave 2 almost identical methods in the test class?