Telerik® JustMock™ by Progress

The DoInstead method is used to replace the actual implementation of a method with a mocked one. This topic goes through a number of scenarios where the DoInstead method is useful.

The following system under test will be used for the examples in this article:

Assert DoInstead

Let's see how to replace a method behavior, verify its call and its return value.

First, we arrange with DoInstead to execute called = true; instead of the actual implementation of foo.Execute method. Also, we set up that the call should return the passed argument directly. We act by calling the foo.Execute method with argument "bar" and then verify that the method actually returns what we expect.

Assert DoInstead for Several Arguments

You can assert DoInstead for more than one argument in the method. Follows an example for asserting four arguments:

Here we replace the actual implementation of the Submit method and return the sum of the specified arguments.

Assert DoInstead On Property Set

DoInstead can also be used to change the behavior of a property set. Here we arrange to set a boolean value instead of actually setting the foo.Bar property.

First, we arrange with DoInstead to execute expected = true; instead of the actual implementation of foo.Bar property setter. We act by setting foo.Bar to 1 and verify that expected is true.


Notice that we use Behavior.Strict for the mocked object.

Using Parameters Passed To The Original Call In DoInstead

DoInstead can also use the parameters passed to the original call. Assume the following examples, with the methods Echo and Bar previously added to the Foo class:

Now, in DoInstead, we will use the integer value passed to Echo to replace the actual implementation:

To parameterise the DoInstead call to apply the Echo method, simply use an action in the body accepting int to access parameters passed to Echo. In the example we replace the actual implementation with actual = a;. We act by calling foo.Echo( expected ); and verify the result.

Additionaly, you can test actions passed to the DoInstead call. Look at the following example:

We act by calling foo.Bar with new Action, changing the actual variable. In DoInstead we just execute the action. Finally, we verify that our expectations are met, namely actual and expected variables have the same value.

Using out and ref Parameters Passed To The Original Call In DoInstead

In order to mock methods containging out and ref parameters, you need to specify a delegate describing your method's prototype. Here is the method we are going to mock. Note that it is virtual and the second parameter is declared as a ref parameter.

The idea behind this method is to have one parameter (arg1) which value is added to the second parameter(arg2). As a result of the method call the value of the second parameter will have changed as long as arg1 is not 0. Let's create a test method that performs this logic using DoInstead.

And here is the delegate we referenced in the DoInstead call:

See Also

Other Resources