Typemock Isolator API Documentation

DynamicReturnValue Delegate

Delegate to implement a custom return value

[Visual Basic]
Public Delegate Function DynamicReturnValue( _
   ByVal parameters As Object(), _
   ByVal context As Object _
) As Object
[C#]
public delegate object DynamicReturnValue(
   object[] parameters,
   object context
);

Parameters

parameters
array of parameters passed to mocked method
context
a reference to the mocked object instance, or null if the method is static

Return Value

custom return value

Remarks

Typemock Isolator allows you to specify mocked return values for mocked methods. These values are normally hard coded, but there are some cases where this is not enough and a custom return value is needed, to do so create a delegation method and pass it as the return value to one of the Mock setup methods

For example, if we need to return the first int parameter of a mocked method, but throw an exception if the value is 0, we can write the following code

// C#
public static object MyReturnValue(object[] parameters, object context) 
{
    if ((int)parameters[0]==0) 
    {
        throw new Exception();
    }
    return parameters[0];
}
' Visual Basic
Public Shared Function MyReturnValue(ByVal parameters() As Object, ByVal that As Object) As Object
    If parameters(0)=0 Then
       Throw New Exception
    End If
    MyReturnValue = parameters(0)
End Function
We can then use it in our test
// C#
[Test]
public void Test() 
{
    // Start mocking TestedClass
    Mock mock = MockManager.Mock(typeof(TestedClass));
    // getInt will be called, it will return our dynamic values
    mock.AlwaysReturn("getInt",new DynamicReturnValue(MyReturnValue));
    TestedClass t = new TestedClass();
    // This will pass
    Assert.AreEqual(10,t.getVar(10));
    // so will this
    Assert.AreEqual(1,t.getVar(1));
    // this will throw an exception
    t.getVar(0);
    MockManager.Verify();
}
' Visual Basic
<Test()> _
Public Sub Test() 
    ' Start mocking TestedClass
    Dim mock As Mock = MockManager.Mock(GetType(TestedClass))
    ' getInt will be called, it will return our dynamic values
    mock.AlwaysReturn("getInt",New DynamicReturnValue(AddressOf MyReturnValue))
    Dim t As TestedClass = New TestedClass()
    ' This will pass
    Assert.AreEqual(10,t.getVar(10))
    ' so will this
    Assert.AreEqual(1,t.getVar(1))
    ' this will throw an exception
    t.getVar(0)
    MockManager.Verify()
End Sub

Note    BEWARE, Typemock Isolator does not check the legitimacy of the returned value, if you return a value with the wrong type a System.InvalidCastException will be thrown
Note    BEWARE, All code that is run inside the delegate is NOT mocked. thrown
Note    To continue with the original method without mocking, return CONTINUE_WITH_METHOD
Note    You can use DynamicReturnValue to change ref and out parameters Just change the values of the parameters array and if the parameter is referenced it will change its value. Again Typemock Isolator does not check the legitimacy of the value.

Requirements

Namespace: TypeMock

Assembly: TypeMock (in TypeMock.dll)

See Also

TypeMock Namespace