Telerik® JustMock™ by Progress

Occurrence is used in conjunction with Mock.Assert and Mock.AssertSet to determine how many times a call has occurred.

There are 6 types of occurrence that we can use:

Furthermore, you can set occurrence directly in the arrangement of a method.

You can use one of 5 different constructs of Occur:

Telerik JustMock also enables you to verify the calls order, with:

  • InOrder() - Specifies exactly the order, a call should occur on the mock.

In the further examples we will use the following interface to test:

Occurs.Never

Occurs.Never is used to verify that a method/property hasn't been called during the execution of the test.

In the example above, we verify that foo.Submit() has never been called.

Occurs.Once

Occurs.Once is used to verify that a method/property has been called exactly one time during the execution of the test.

In the example above, we verify that foo.Submit() has been called once.

Occurs.AtLeastOnce

Occurs.AtLeastOnce is used to verify that a method/property has been called at least once during the execution of the test.

The example above shows that the Occurs.AtLeastOnce requirement is met whenever the method is called one or more times.

Occurs.AtLeast(numberOfTimes)

With Occurs.AtLeast you specify at least how many times you want to verify that a given method/property has been called during the execution of the test.

Important

In the next example is used NUnit Testing Framework.

In the example above, the first Assert throws an exception as the foo.Submit method has been called only twice at that time, but the Occurs.AtLeast requires the method to be called at least 3 times. Afterwards, the method is called once again which causes the conditions in the second Assert to be satisfied.

Occurs.AtMost(numberOfTimes)

With Occurs.AtMost you specify at most how many times you want to verify that a given method/property has been called during the execution of the test.

In the example above, the first Assert passes as the foo.Submit method has been called only twice. However, the second Assert throws an exception because the same method has been called 3 times in total, while the condition is to be called at most 2 times.

Occurs.Exactly(numberOfTimes)

With Occurs.Exactly you specify exactly how many times you want to verify that a given method/property has been called during the execution of the test.

Important

In the next example is used NUnit Testing Framework.

In the example above, the first Assert throws an exception because we call foo.Submit less times than the Occurs.Exactly condition specifies. The second Assert passes because there are exactly 3 calls to foo.Submit method. The third Assert throws an exception again, because at than time the foo.Submit method is called 4 times, which is more that what is specified in the Occurs.Exactly condition.

Occurs(numberOfTimes)

With Occurs(numberOfTimes) you specify exactly how many times you want to verify that a given method/property has been called during the execution of the test.

Important

In the next example is used NUnit Testing Framework.

In the example above, Occurs(2) marks the call is required to be executed 2 times, therefore Mock.Assert will fail if not expected properly.

OccursOnce()

OccursOnce() is used to verify that a method/property has been called exactly one time during the execution of the test.

In the example above, we arrange that foo.Submit() must be called exactly once.

OccursNever()

OccursNever() is used to verify that a method/property hasn't been called during the execution of the test.

In the example above, we verify that foo.Submit() has never been called.

OccursAtLeast(numberOfTimes)

With OccursAtLeast(numberOfTimes) you specify at least how many times you want to verify that a given method/property has been called during the execution of the test.

In the example above, foo.Submit() is called 3 times. The verification will not fail as it is expected to execute at least 2 times.

OccursAtMost(numberOfTimes)

With OccursAtMost(numberOfTimes) you specify at most how many times you want to verify that a given method/property has been called during the execution of the test.

In the example above, we specify that foo.Submit() must be called at most 2 times. As it is called 3 times, AssertFailedException will be thrown immediately after the third call.

Asserting Multiple Occurrences

JustMock enables you to assert multiple occurrences of similar type of call using Matcher in Mock.Assert rather than asserting each of them separately.

Here is an example:

Our arrangement is for calling Echo method with 1, 2 or 3 as argument. In assertion we use a matcher to cover all calls with integer as argument. As calling foo.Echo with 1, 2 and 3 in acting phase applies that matcher, a call with integer occurred exactly 3 times.

Refer to Matchers for more information about using matchers.

Verifying Calls Order

To ensure that a set of method calls are executed in a particular order, you can use the InOrder() method. To achieve this, you will need to use the following workflow:

  1. You arrange the methods to define their invocation order;
  2. You act;
  3. You assert on the mock, to check if the method execution order is as expected.

Next is an example, showing InOrder() use case:

Note that the InOrder option also supports asserting the order of mock calls regardless of the instance within the test scope. Imagine that you have to validate that the user has logged in before using his/hers shopping cart in your application.

Here we have defined the IUserValidationService and the IShoppingCartService services whose invocation order we are going to assert in the following test:

In the arrange phase we have defined that the ValidateUser call should be made only once and before the LoadCart service call. The LoadCart call should also occur only once and should follow the ValidateUser service call. We act and then assert our expectations.

See Also