Hi,
Thanks for previous answer. My next question is: is it possible to mock only one (or selected) instance method.
I want to test method:
public static int ExecuteCommandWithReturnValue(string storedProcedure, ref SqlParameter[] arParams, int commandTimeout, string connectionString)
{
int returnValue = 0;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(storedProcedure, connection);
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = commandTimeout;
foreach (SqlParameter parameter in arParams)
{
command.Parameters.Add(parameter.ParameterName, parameter.SqlDbType);
command.Parameters[parameter.ParameterName].Value = parameter.Value;
command.Parameters[parameter.ParameterName].Direction = parameter.Direction;
}
connection.Open();
command.ExecuteNonQuery();
returnValue = (int) command.Parameters[0].Value;
connection.Close();
return returnValue;
}
and prepare test:
SqlParameter[] arParams = new SqlParameter[] { new SqlParameter("par1", "val1") };
arParams[0].Direction = ParameterDirection.Input;
arParams[0].DbType = DbType.String;
using (RecordExpectations r = RecorderManager.StartRecording())
{
SqlConnection connection = new SqlConnection(null);
connection.Open();
//command.ExecuteNonQuery(); how to mock only this method
//r.Return(2);
connection.Close();
}
Assert.IsInstanceOfType(typeof(int), Common.ExecuteCommandWithReturnValue("storedProcedure", ref arParams, 2, "connectionString"));
I want to "command" instance do all things as is in tested method and only mock invoking command.ExecuteNonQuery. Is it possible?
When i try put in using clausule:
SqlCommand command = new SqlCommand(null,null);
i must mock all "command" invocation also:
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 2;
command.Parameters.Add("par1", SqlDbType.VarChar);
r.Return(new SqlParameter());
command.Parameters["par1"].Value = "val1";
command.Parameters["par1"].Direction = ParameterDirection.Input;
It is lot of unnecessary code, I think.
br
Tadeusz