Telerik® JustMock™ by Progress

Recursive mocks enable you to mock members that are obtained as a result of "chained" calls on a mock. For example, recursive mocking is useful in the cases when you test code like this: foo.Bar.Baz.Do("x").

In the further examples we will use the following sample code to test:

Step by step example

Consider the above code. Let's arrange that a call to IFoo.Do method returns a particular string. IFoo contains an IBar which, in turn, also contains a Do method. IBar.Do method can be accessed via a recursive call to IFoo.Bar. To arrange the call directly from IFoo, we just need to chain it considering the fact that Telerik JustMock will automatically create the necessary mock for IBar.

However, if foo.Bar is not arranged, it will still be instantiated. You can verify this by the following example:

Nested Property Calls

The following example shows how to assert nested property get calls.

Here we arrange foo.Bar.Value to return 10.

You can also arrange property set. Here is an example:

We use Bahavior.Strict to enable only arranged calls and to reject any other calls. Only setting foo.Bar.Value to 5 is allowed and as we set it to 10, an exception will be thrown. After assertion we actually set it to 5 and if we verify foo at that point an exception won't be thrown.

Nested Property and Method Calls

We can call properties recursively, or we can do it with methods as well.

In this arrangement we specify that when calling foo.Bar.Do and foo.Bar.Baz.Do methods "xit" and "yit" should be returned respectively.

See Also

Other Resources