Arranging a Fake

The next step using fake-objects in the unit test is to arrange rules for calls its members. There is a special static class Arrange using for it. The following sections describe how to arrange different rules for the fake-object.

Returning Value

If you want to simulate the method returns a value, you can do this as follows:

Arrange.Call(() => shapeFake.GetThickness()).Returns(2);

Now GetThickness() method of shapeFake object will return “2”.

Similarly, you can create a rule for the property or indexer get accessor :

Arrange.Call(() => shapeFake.Name).Returns("Blue circle");

Returns ref and out Arguments

You can also simulate the behavior of the method with input out and ref arguments by setting custom values for it, as in the example below:

int width;
int height;
Arrange.Call(() => shapeFake.GetDims(out width, out height)).SetOutAndRefArguments(20, 30);

Thus, when you call GetDims method of shapeFile variable width will set to “20” and the variable height to “30”.

If the method contains both out/ref arguments and return parameter, you should set return parameter first and then out/ ref arguments.

Throwing Exception

You can simulate the exception throwing in the called method or property by specifing the exception type that you want to throw in the Throws method :

Arrange.Call(() => shapeFake.CanSelect(-1, -1)).Throws<ArgumentException>();

or directly pass the Exception object:

var exception = new ArgumentException("Invalid position parameter.");
Arrange.Call(() => shapeFake.CanSelect(-1, -1)).Throws(exception);

Executing Callback

True Fake provides the capability to perform a custom action when you call the fake-object method or property, passing a delegate to the Do method:

Arrange.Call(() => shapeFake.Draw()).Do(() => Console.WriteLine("Drawing shape."));

In this example, when you call Draw() of shapeFake object it will write to the console.

Arranging Property Set Accessor

Simulating the behavior of the set accessor shown in the following example. It throws an exception whenever passed incorrect figure name:

Arrange.PropertySet(() => shapeFake.Name).WhenSet("*^&IncorrectName").Throws<ArgumentException>();

If your class for some incredible reason have a property which have only set, but have no get accessor, you can use the following code to arrange this property.

Arrange.PropertySet(f, nameof(shapeFake.Name)).WhenSet("*^&IncorrectName").Throws<ArgumentException>();

Argument Matching

You can specify different behavior for the same method with different input parameters as follows:

Arrange.Call(() => shapeFake.CanSelect(0, 10)).Returns(true);
Arrange.Call(() => shapeFake.CanSelect(10, 20)).Returns(false);

However, if you write a rule to simulate the behavior of the method with the same input parameters, such as:

Arrange.Call(() => shapeFake.CanSelect(0, 10)).Returns(true);
Arrange.Call(() => shapeFake.CanSelect(0, 10)).Returns(false);

then it will be carried out the only first rule.


While arranging you can flexibly set the rules for the fake-object method input parameters. You should use methods of Arg static class for this.

Setting the method behavior regardless of any parameter is carring out as follows:

Arrange.Call(() => shapeFake.CanSelect(Arg<int>.Any, 10)).Returns(true);

For int, char, byte, decimal, short, double, string, uint, ushort, float, ulong, bool, long, sbyte there are implicit operators overloaded, so the example can be rewritten as follows:

Arrange.Call(() => shapeFake.CanSelect(Arg.Any, 10)).Returns(true);

There is one more way to ignore the input parameters of the arranged method is to use IgnoreArguments() method, as in the example below:

Arrange.Call(() => shapeFake.CanSelect(0, 0)).IgnoreArguments().Returns(true);

While arranging method call you can specify rules for input parameters using a predicate delegate as follows:

Arrange.Call(() => shapeFake.CanSelect(Arg<int>.Is(t => t < 0), Arg<int>.Is(t => t < 0))).Throws<ArgumentException>();

If the method has ref input parameters, you can arrange it in such a way:

Arrange.Call(() => shapeFake.ClarifyCenter(ref Arg<int>.RefIs(t => t == 5).Ref, ref Arg<int>.RefIs(t => t == 10).Ref)).Throws<ArgumentException>();

You can also set rules for the values passed to the property as follows:

Arrange.PropertySet(() => shapeFake.Name).WhenSet(Arg.Any).Do(() => Console.Write("Name successfully set."));

NOTE: Arrangements for fake-object members are matching in the same order in which they were defined in the code. In this regard, you must specify a more custom arguments first, and then a more common arguments, for example:

Arrange.Call(() => shapeFake.CanSelect(5, 7)).Returns(true);
Arrange.Call(() => shapeFake.CanSelect(Arg<int>.Is(t => t < 0), Arg<int>.Is(t => t < 0))).Throws<ArgumentException>();
Arrange.Call(() => shapeFake.CanSelect(Arg.Any, Arg.Any)).Returns(false);

Thus, if you arrange method call with any arguments first:

Arrange.Call(() => shapeFake.CanSelect(Arg.Any, Arg.Any)).Returns(false);

and after that arrange it with all other rules, then arranged method call will behave according to the first arrangement.