On Jun 25, 4:12 pm, Kipp  Howard <[email protected]> wrote:
> I have a helper method to setup my Expect statement.  The helper
> method takes an array of SqlParameters.  I've tried just about
> everything to get the method stub to return a specific value (entity)
> when a certain set of SqlParameters are passed in.  I know the below
> is not going to work because the SqlParameter[] items passed in are
> not the same objects as are being past to the mock object although
> their order, type and value are all identical.
>
> private static void ExecuteEntityMock<T>(
>       T entity,
>       string sql,
>       params SqlParameter[] cmdParams)
>  {
>     iSqlHelper.Expect(mp => mp.ExecuteEntity<T>(
>        Arg<String>.Is.Anything,
>        Arg<CommandType>.Is.Anything,
>        Arg.Text.Contains(sql),
>        Arg<SqlParameter[]>.List.Equal(cmdParams))) // Doesn't work
>        .Repeat.Once()
>        .Return(entity);
>  }

I have resolved my issue by creating a SqlParameterArrayConstraint.
I'd like to hear if I'm doing anything exceptionally wrong here or is
this the typical resolution of this problem (as I'm starting to see).
Sorry this was more of a FAQ than I thought it was at first.

private static void ExecuteEntityMock<T>(
      T entity,
      string sql,
      params SqlParameter[] cmdParams)
{
    iSqlHelper.Expect(mp => mp.ExecuteEntity<T>(
       Arg<String>.Is.Anything,
       Arg<CommandType>.Is.Anything,
       Arg.Text.Contains(sql),
       Arg<SqlParameter[]>.Matches(new
SqlParameterArrayConstraint(cmdParams))))
       .Repeat.Once()
       .Return(entity);
}

The SqlParameterArrayConstraint looks like:

   public class SqlParameterArrayConstraint : AbstractConstraint
   {
      private SqlParameter[] sqlParameters;
      /// <summary>
      /// Constructor to create constraint
      /// </summary>
      /// <param name="sqlParams"></param>
      public SqlParameterArrayConstraint(SqlParameter[] sqlParams)
      {
         sqlParameters = sqlParams;
      }

      public override bool Eval(object obj)
      {
         if (!(obj is SqlParameter[]))
         {
            return false;
         }

         SqlParameter[] sqlParams = (SqlParameter[])obj;
         if (sqlParameters.Length != sqlParams.Length)
         {
            return false;
         }

         for (int i = 0; i < sqlParams.Length; i++)
         {
            SqlParameter expected = sqlParameters[i];
            SqlParameter actual = sqlParams[i];
            // verify type and value (as string)
            // Using ToString() to simplify code.  Not optimum
comparison.
            if (expected.SqlDbType != actual.SqlDbType ||
               expected.Value.ToString() != actual.Value.ToString())
            {
               return false;
            }
         }
         return true;
      }

      public override string Message
      {
         get
         {
            return "SqlParameter arrays do not match.";
         }
      }
   }

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to