JustMock

Sequential mocking allows you to return different values on the same or different consecutive calls to one and the same type. In other words, you can set up expectations for successive calls of the same type.

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

Step by step example

Consider the above code. We have a simple interface named IFoo and it has a method named GetIntValue that returns int. We want to make sure that on three different successive calls, it returns three different predefined values.

In the arrange section, we setup that:

  • the first call to GetIntValue returns 0
  • the second call returns 1
  • the third call returns 2
We achieve that by just calling InSequence() on the Arrange. This modifier instructs Telerik JustMock to return the expected result in the order you specify.

Note

In this example, every subsequent call to foo.GetIntValue will result in returning the last specified arrangement, namely 2.

Assert Sequence with a Matcher

You can arrange consecutive calls to one and the same method passing different arguments to return different values.

In sequential mocking you can still use the power of other features, like Matchers, to write more complete and precise tests.

Important
Remember that if you arrange two calls, but actually perform more than two calls, all calls after the second one will return the value specified to be returned in the second arrange.

Assert Multiple Calls with Different Matcher

To extend the previous example, we can arrange consecutive calls to methods to return different values depending on a condition specified with a matcher.

Returns and InSequence

When using Returns you can chain calls like in the following example of InSequence alternative.

Note

This feature is enabled when Telerik.JustMock.Helpers namespace is included.

The effect is the same as using separate arranges for the foo object. The first call will return 10 and the second - 11.

Another possible approach is to use the ReturnsMany helper method, like so:

Note

This feature is enabled when Telerik.JustMock.Helpers namespace is included.

The ReturnsMany method will arrange certain function/property to sequentially return the members of a predefined array (returnValues). If the calls to that function/property exceed the array members during the act phase, the last arranged value will become default. This is shown in the next example:

See Also