Hi,
I'm evaluating TypeMock for my projects and ran into astrange problem when trieing to mock Database calls
Here my code i want to test:
public DataTable GetData()
{
SqlCommand c = SPCommand(SP_GetData);
int res;
c.Connection.Open();
SqlDataAdapter ad = new SqlDataAdapter(c);
DataTable ret = new DataTable();
try
{
res = ad.Fill(ret);
}
finally
{
c.Connection.Close();
}
return ret;
}
and here my UnitTest:
[Test]
[VerifyMocks]
public void GetData_Test()
{
string spName = "SP_GetData";
using (RecordExpectations rec = RecorderManager.StartRecording())
{
rec.DefaultBehavior.CheckArguments();
rec.VerifyMode = VerifyMode.Normal;
var conn = new SqlConnection(fakeConnStr);
var cmd = new SqlCommand
{
Connection = conn,
CommandText = spName,
CommandType = CommandType.StoredProcedure
};
cmd.Connection.Open();
Mock SqlDataAdapterMock = MockManager.MockAll<SqlDataAdapter>();
SqlDataAdapterMock.ExpectAndReturn("Fill", new DynamicReturnValue(MockFill));
DataTable dtDummy = new DataTable();
cmd.Connection.Close();
}
var t = new DALClass(fakeConnStr);
DataTable dt = t.GetSynchronisationErrors();
Assert.That(dt.Columns["Col1"].DataType == typeof(int));
Assert.That(dt.Columns["Col2"].DataType == typeof(char));
Assert.That(dt.Columns["Col3"].DataType == typeof(string));
}
public object MockFill(object[] parameters, object context)
{
DataTable dt = (DataTable) parameters[0];
dt.Columns.Add("Col1",typeof(int));
dt.Columns.Add("Col2",typeof(char));
dt.Columns.Add("Col3",typeof(string));
dt.Rows.Add(new object[] { 1, '1', "1" });
dt.Rows.Add(new object[] { 2, '2', "2" });
dt.Rows.Add(new object[] { 3, '3', "3" });
return 3;
}
When running my test i get the following TypeMockException:
TypeMock.TypeMockException:
*** Cannot return a value for IpcClientChannel.get_ChannelName() because no value was set. use recorder.Return().
What i'm doing wrong? as far as i understand the lines:
Mock SqlDataAdapterMock = MockManager.MockAll<SqlDataAdapter>();
SqlDataAdapterMock.ExpectAndReturn("Fill", new DynamicReturnValue(MockFill));
should instruct to mock the SqlDataAdapter and fill the DataTable passed in my code with the MockFill Method.
thanks in advance for your help,
chris