Hi Scott,
SqlCommand mockSqlCommand = new SqlCommand();
In my opinion, the only mock this line has is the variable name.
But, I understand that you can add
Mock realMockSqlCommand = MockManager.Mock(typeof(SqlCommand))
This works fine and I admit that I didn't think about it.
My solution was to generalize my code so that I could use only interfaces (IdbConnection, IdbCommand).
Here is some of the code:
// test Init code
MockManager.Init();
m_MockConnection = MockManager.MockObject(typeof(IDbConnection));
m_MockConnection.ExpectCall("Open");
m_MockConnection.ExpectGet("State",ConnectionState.Open);
Mock factory = MockManager.Mock(typeof(DatabaseProviderFactory));
factory.ExpectAndReturn("CreateConnection",m_MockConnection.Object);
...
// test code
MockObject mockReader = MockManager.MockObject(typeof(IDataReader));
mockReader.ExpectAndReturn("Read",true);
mockReader.ExpectAndReturn("GetString","data",4);
mockReader.ExpectAndReturn("Read",false);
mockReader.ExpectCall("Close");
MockObject mockCommand = MockManager.MockObject(typeof(IDbCommand));
IDbCommand command = (IDbCommand) mockCommand.Object;
mockCommand.ExpectSet("CommandText");
mockCommand.ExpectAndReturn("ExecuteReader",mockReader.Object);
m_MockConnection.ExpectAndReturn("CreateCommand",command);
The benefit of using the interface is that my test fixture work for any type of database now not only SQL Server. I used a factory class to create the database specific connection (see init code above).
Anywayz, that was my "Workaround" that lead me to a better design of my code.
A happy ending for me. :D
BTW, It is "day and night" for me with version 2.2.
This database test fixure failed at the first lines with version 2.1.
Keep up the good work.
Best Regards,
Benjamin