chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I'm Trying to mock a method that requires a SqlCommand object as the first parameter.

Here is my test code:
[Test]
        public void ExecuteNonQueryWithStrConnAndProcNameAndSqlParams()
        {
            SqlConnection sqlConn = new SqlConnection();
            SqlCommand sqlCmd = new SqlCommand();
            
            Mock prepCmdMock = MockManager.Mock(typeof(SqlHelper));
            prepCmdMock.ExpectUnmockedCall("ExecuteNonQuery").Args(sqlConn, "ProcedureName", (SqlParameter[])null);
            prepCmdMock.ExpectCall("PrepareCommand").Args([color=darkred]sqlCmd[/color], sqlConn, (IDbTransaction)null, "ProcedureName", (SqlParameter[])null);
            SqlHelper.ExecuteNonQuery(sqlConn, "ProcedureName", (SqlParameter[])null);
        }


Here is the method under test:

public static int ExecuteNonQuery(SqlConnection Connection, string ProcedureName, SqlParameter[] CommandParameters)
      {   

         //create a command and prepare it for execution
         SqlCommand cmd = new SqlCommand();
         PrepareCommand([color=darkred]cmd[/color], Connection, (IDbTransaction)null, ProcedureName, CommandParameters);
         
         //finally, execute the command.
         int retval = cmd.ExecuteNonQuery();
         
         // detach the SqlParameters from the command object, so they can be used again.
         cmd.Parameters.Clear();
         return retval;
      }


This test failes with the following output:

TestCase 'QC.DAL.Tests.sqlHelperFixture.ExecuteNonQueryWithStrConnAndProcNameAndSqlParams'
failed: TypeMock.VerifyException : 
TypeMock Verification: Call to QC.DAL.SqlHelper.PrepareCommand() Parameter: 1
    expected: [System.Data.SqlClient.SqlCommand]
     but was: [System.Data.SqlClient.SqlCommand]
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Object[] A_4)
   at TypeMock.InternalMockManager.getReturn(Object that, String 
...


Am I missing something? The Expected and Actual are both reported as SqlCommand objects.
asked by IfValueEqNull (1.1k points)

3 Answers

0 votes
Hi,

When checking for arguments TypeMock verifies that the object expected is .Equals() to the actual object that was passed.
In your case the objects are not the same instance and so the test fails. (:idea: We should have a better message)

The best way to solve this is to understand what we are testing for.
Are we testing to see that a certian SqlCommand has been passed? or do we expect a certian instance? perhaps we just want to test that the argument is an SqlCommand type, or perhaps we don't really want to test this argument.

Lets see how we will test each scenario:
1. we don't really want to test this argument.
.Args(Check.IsAny(),...)

2. the argument is an SqlCommand type
.Args(Check.IsTypeOf(typeof(SqlCommand )),...)

3. a certian SqlCommand has been passed, or expect a certian instance
This depends, if IsEquals is implemented we can use it or we can use our own test function
.Args(new ParameterCheckerEx(CheckParameter),...)
...
public static bool CheckParameter(ParameterCheckerEventArgs data)
{
   SqlCommand cmd = data.ArgumentValue as SqlCommand;
   // test our cmd here...
   return result;
}



You can see more information in the Built in argument Check Documentation
answered by scott (32k points)
0 votes
Hi,

When checking for arguments TypeMock verifies that the object expected is .Equals() to the actual object that was passed.
In your case the objects are not the same instance and so the test fails. (:idea: We should have a better message)

...
2. the argument is an SqlCommand type
.Args(Check.IsTypeOf(typeof(SqlCommand )),...)

...



Yes. I had a sneaking suspicion this was the case. In my case I just need to know that it is of type SqlCommand so step number 2 in your example has solved the problem. Thanks for curbing my ignorance. :?'
answered by IfValueEqNull (1.1k points)
0 votes
We have added an better message in version 3.5 that will explicitly help in these cases.
Thanks for pointing this out.
answered by scott (32k points)
...