Asserting Occurrence
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:
- Occurs.Never() - Specifies that a particular call is never made on a mock.
- Occurs.Once() - Specifies that a call has occurred only once on a mock.
- Occurs.AtLeastOnce() - Specifies that a call has occurred at least once on a mock.
- Occurs.AtLeast(numberOfTimes) - Specifies the least number of times a call should occur on a mock.
- Occurs.AtMost(numberOfTimes) - Specifies the most number of times a call should occur on a mock.
- Occurs.Exactly(numberOfTimes) - Specifies exactly the number of times a call should occur on a mock.
Furthermore, you can set occurrence directly in the arrangement of a method.
You can use one of 5 different constructs of Occur:
- Occurs(numberOfTimes) - Specifies the exact number of times a call should occur on a mock.
- OccursOnce() - Specifies that a call should occur only once on a mock.
- OccursNever() - Specifies that a particular call should never be made on a mock.
- OccursAtLeast(numberOfTimes) - Specifies that a call should occur at least once on a mock.
- OccursAtMost(numberOfTimes) - Specifies the number of times at most a call should occur on a mock.
JustMock also enables you to verify the calls order, with:
- InOrder() - Specifies exactly the order, a call should occur on the mock.
In addition to that JustMock allows you to verify that method call prerequisites are satisfied
- AfterAll(prerequisites) - Specifies prerequisites which should be met before a method call.
In the following 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.
The next example uses 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 2 times at most.
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.
The next example uses 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.
The next example uses 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:
- You arrange the methods to define their invocation order.
- You act.
-
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.
Verifying Call Prerequisites
JustMock gives you an ability to ensure that a method can be called only after one or more prerequisites have been called in prior. To achieve this use case, the following workflow is applicable:
- Arrange the methods to define the prerequisites.
- Specify the list all required prerequisites in
AfterAll
. -
Assert the target mock, to verify that all expected prerequisites have been called.
Next is an typical example, showing
AfterAll()
in action. Imagine that you have the following interfaces:and you need to verify that
FooContainer.Elements
is called after allFoo.Init
have beeen called, than the following test can help: