Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters Faking Ref and Out Parameters

Faking Ref and Out Parameters

To return Ref and Out parameters, set the value of these arguments before faking the call to the method.

When to Use

You can fake Ref and Out parameters when your test requires a specific return value from them.

Syntax

C#

var outValue = <value>;
Isolate.WhenCalled(() => fake.SomeMethod(ref outValue)).<behavior>

VB

Dim outValue = <value>
Isolate.WhenCalled(Function fake.SomeMethod(outValue)).<behavior>

Samples

The following sample shows how to fake the first and second parameter of SomeMethod() as follows:

Required return values are assigned to the variables.

The variables are passed to the fake method in Isolate.WhenCalled().

C#

[TestMethod, Isolated]
public void RefArgument_Example()
{
  string outStr = "typemock";
  List<int> outList = new List<int>() { 100 };
  var fake = new Dependency();
  Isolate.WhenCalled(() => fake.SomeMethod(ref outStr, out outList)).IgnoreCall();

  var result = new ClassUnderTest().GetString(fake);

  Assert.AreEqual("typemock1", result);
}

public class ClassUnderTest
{
  public string GetString(Dependency dependency)
  {
    var name = "unit testing";
    List<int> list;
    dependency.SomeMethod(ref name, out list);

    return name + list.Count.ToString();
  }
}

public class Dependency
{
  public virtual void SomeMethod(ref string name, out List<int> list)
  {
    throw new NotImplementedException();
  }
}

VB

<TestMethod, Isolated>
Public Sub RefArgument_Example()
    Dim outStr As String = "typemock"
    Dim outList As New List(Of Integer)() From {100}
    Dim fake = New Dependency()
	
    Isolate.WhenCalled(Sub() fake.SomeMethod(outStr, outList)).IgnoreCall()

    Dim result = New ClassUnderTest().GetString(fake)

    Assert.AreEqual("typemock1", result)
End Sub


Public Class ClassUnderTest
    Public Function GetString(dependency As Dependency) As String
        Dim name = "unit testing"
        Dim list As List(Of Integer)
        dependency.SomeMethod(name, list)

        Return name + list.Count.ToString()
    End Function
End Class

Public Class Dependency
    Public Overridable Sub SomeMethod(ByRef name As String, ByRef list As List(Of Integer))
        Throw New NotImplementedException()
    End Sub
End Class