Member Avatar for pilsdumps

Hi,

I've a unit test for a protected method that contains a ref parameter. The method to test looks like this:

protected void Select(ref int? intReturnValue,
                          ModelBase modelbase)
    {
...some code....
          //add the return value
          SqlParameter paramReturn = new SqlParameter("@ReturnValue", 0);
          paramReturn.Direction = ParameterDirection.ReturnValue;
          sqlComm.Parameters.Add(paramReturn);

          //run the proc
          sqlConn.Open();
          sqlComm.ExecuteNonQuery();

          //get the return value                           
          intReturnValue = Int32.Parse(sqlComm.Parameters["@ReturnValue"].Value.ToString());

The app checks the intReturnValue for the success or failure of the stored procedure run.

To test this method, I have the following test;

[TestMethod()]
    [DeploymentItem("TestNav.exe")]
    public void SelectTest()
    {        
      int? intReturnValue = -100;
      int? intReturnValueExpected = 0;
      ModelBase modelbase = CreateNewPatient();

      privateobject.Invoke("Select",
                            new Type[] { typeof(int?).MakeByRefType(), typeof(ModelBase) },
                            new object[] { intReturnValue, modelbase });
      
      Assert.AreEqual(intReturnValueExpected, intReturnValue);

    }

The problem is that despite passing the intReturnValue by ref using the MakeByRefType() method (or at least attempting to if I have done it correctly), intReturnValue does not update. I have set it's initial value to -100 as this is never returned by the stored proc, but the test always fails - ie. intReturnValue never receives the stored proc return value.

Can anyone please show me where I'm going wrong with this?

Hello,
unfortunatelly I have no tools to test your code right now .. but I want share some thoughts about that.

I suppose that, that might be a problem with boxing of a ref-type variable when passing it in parameters list (by the way, have you tried passing it as "ref intReturnValue", rather than as "intReturnValue"). Anyways - take at look at this discussion: C# 4.0 ‘dynamic’ doesn’t set ref/out arguments.

Retrieve the return value from the argument array of the Invoke.

[TestMethod()]
[DeploymentItem("TestNav.exe")]
public void SelectTest()
{
  int? intInValue = -100;
  int? intReturnValueExpected = 0;
  ModelBase modelbase = CreateNewPatient();

  Type[] myTypes = new Type[] { typeof(int?).MakeByRefType(), typeof(ModelBase) };
  object[] myArgs = new object[] { intInValue, modelbase };

  privateobject.Invoke("Select", myTypes, myArgs); 

  int? intReturnValue = (int?)myArgs[0];

  Assert.AreEqual(intReturnValueExpected, intReturnValue);

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.