We’ve just separated our SQL Server database adapters to allow for differences between SQL Server 2000 and 2005, specifically, the nvarchar(max) data type.
Because the code change is so small, instead of reimplementing our adapter interfaces and abstract base class, I subclassed the SQL 2005 adapter from SQL 2000 and just override the appropriate methods and properties.
public abstract class SqlDatabaseAdapter : DatabaseAdapter, ISqlDatabaseAdapter
...
public class Sql2000DatabaseAdapter : SqlDatabaseAdapter
...
public class Sql2005DatabaseAdapter : Sql2000DatabaseAdapter
...
Our unit test code previously just cast to a particular type of adapter and then made an assertion against that type, but now that we have an adapter factory method to build the appropriate SQL Server adapter (from configuration or found via SQL connection), I want to verify that the exact type is instantiated (since we now default to SQL Server 2005), the only problem is our old tests are passing when they should be failing 🙂
In the example below, the entity factory creates a Sql2005DatabaseAdapter, but the test passes because this type inherits from Sql2000DatabaseAdapter. And it also works further up the ancestor chain.
Entity foo = EntityFactory.Create("foo");
Assert.IsInstanceOfType(typeof(SqlDatabaseAdapter), foo.Adapter); // Should fail for test
Assert.IsInstanceOfType(typeof(Sql2000DatabaseAdapter), foo.Adapter); // Should fail for test
Assert.IsInstanceOfType(typeof(Sql2005DatabaseAdapter), foo.Adapter); // Should pass
I understand why it’s passing, and I can use the general Assert.AreEqual method to fix this particular case.
Assert.AreEqual(typeof(Sql2005DatabaseAdapter), foo.Adapter.GetType());
But I’m curious if there’s a way using Assert.IsInstanceOfType?
Since your
Sql2005DatabaseAdapterpresumably cannot work with a SQL 2000 server, the inheritance relationship is just wrong, since it effectively says “Sql2005DatabaseAdapteris aSql2000DatabaseAdapter“, thus leading to all kinds of weirdness, like the problem you are currently encountering in your test.If you want to avoid this, it would be good to move most of the functionality into an abstract base class and have both inherit directly from that: