With Telerik JustMock you can mock delegates and additionally use all mock capabilities on them.
For example, you can assert against their invocation, arrange certain expectations and then pass them
to the system under test. The next test methods should further explain how this is done:
Asserting Delegate Occurrences
Let's assume we have the following Foo class:
To test if the FuncDelegate is called inside the GetInteger method, we can
apply the next test:
It will assert that a Func<int, int> is called at least once, (no matter the integer argument)
during the execution of the test method.
Arranging Return Expectation
Having the same Foo class, as in the above section:
To test the GetInteger method in different scenarios, we may need to arrange a specific behavior to
the FuncDelegate. This can be done like this:
The test arranges the Func<int, int> delegate to return 20 whenever it is called with 10 as an
argument. To assert this expectation, next we act on the system under test.
Passing Delegate Mocks in the System Under Test
For the next examples is used the following DataRepository class:
The first test method will explain how to pass a prearranged delegate mock in the above system under test:
The second test method will explain how to pass a delegate mock in the above system under test and assert its occurrence
instead of invoking its original logic during the test execution: