Mocking Delegates
With Telerik® JustMock you can mock delegates and 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. This topic explains how you can use that functionality.
Sample Setup
For the examples in this topic, let's assume we have the following Foo class:
Sample setup
public class Foo
{
public Func<int, int> FuncDelegate { get; set; }
public int GetInteger(int toThisInt)
{
return FuncDelegate(toThisInt);
}
}Asserting Delegate Occurrences
You can use JustMock to verify that a delegate is actually called, as demonstrated in Example 1.
Example 1: Test that a delegate inside a method is actually called
[TestMethod]
public void ShouldArrangeOccurrenceExpectation()
{
// Arrange
var delegateMock = Mock.Create<Func<int, int>>();
Mock.Arrange(() => delegateMock(Arg.AnyInt)).MustBeCalled();
// Act
var foo = new Foo();
foo.FuncDelegate = delegateMock;
var actual = foo.GetInteger(123);
// Assert
Mock.Assert(delegateMock);
}The test from Example 1 asserts that, during the execution of the test method, Func<int, int> is called at least once no matter the integer argument.
You can also verify the order and the number of invocations. For more details about that functionality, check the Asserting Occurrence topic.
Arranging Return Expectation
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:
Example 2: Change the return value of a delegate
[TestMethod]
public void ShouldArrangeReturnExpectation()
{
// Arrange
var delegateMock = Mock.Create<Func<int, int>>();
// Arrange the `Func<int, int>` delegate to return 20 whenever it is called with 10 as an argument.
Mock.Arrange(() => delegateMock(10)).Returns(20);
// Act
var foo = new Foo();
foo.FuncDelegate = delegateMock;
var actual = foo.GetInteger(10);
// Assert
Assert.AreEqual(20, actual);
}Passing Delegate Mocks in the System Under Test
For the examples in this section, the following DataRepository class is used:
Sample setup
public class DataRepository
{
public string GetCurrentUserId(Func<string> callback)
{
return callback();
}
public void ApproveCredentials(Action<int> callback)
{
// Some logic here...
callback(1);
}
}The first test method will explain how to pass a prearranged delegate mock in the above system under test:
Example 3: Passing a delegate mock as a method argument
[TestMethod]
public void ShouldPassPrearrangedDelegateMockAsArgument()
{
// Arrange
var delegateMock = Mock.Create<Func<string>>();
Mock.Arrange(() => delegateMock()).Returns("Success");
// Act
var testInstance = new DataRepository();
var actual = testInstance.GetCurrentUserId(delegateMock);
// Assert
Assert.AreEqual("Success", actual);
}The second test method demonstrates 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.
Example 4: Passing a delegate mock as a method argument and asserting its occurrence
[TestMethod]
public void ShouldPassDelegateMockAsArgumentAndAssertItsOccurrence()
{
bool isCalled = false;
// Arrange
var delegateMock = Mock.Create<Action<int>>();
// Arrange that instead of invoking the original implementation of the delegate,
// it should only set isCalled to true when invoked.
Mock.Arrange(() => delegateMock(Arg.AnyInt)).DoInstead(() => isCalled = true);
// Act
var testInstance = new DataRepository();
testInstance.ApproveCredentials(delegateMock);
// Assert
Assert.IsTrue(isCalled);
}