background

Telerik JustMock

Mock Delegates

  • JustMock enables you to use mock delegates in your favorite .NET language.
  • Part of the fastest, most flexible and complete mocking tool for crafting unit tests.
  • Our award-winning support team is available to assist you with any issues.
Mock everything
  • Mocking Delegates Overview

    JustMock allows you to use the framework's full capabilities for mocking delegates in your favorite .NET language, whether it is C# or VB. For example, you can arrange a delegate behavior with predefined expectations and assert against its invocation.
  • Return a Predefined Value

    When you isolate your test case from a call to a delegate, a common requirement is that the delegate returns a predefined value.

    public class Foo
    {
        public Func<int, int> FuncDelegate { get; set; }
      
        public int GetInteger(int toThisInt)
        {
            return FuncDelegate(toThisInt);
        }
    }
     
    ...
     
    [TestMethod]
    public void ShouldArrangeReturnExpectation()
    {
        // ARRANGE
        // Creating a mock instance of the Func<int, int> delegate.
        var delegateMock = Mock.Create<Func<int, int>>();
         
        // Arranging:   When the mock is called with 10 as an integer argument,
        //              it should return 20.
        Mock.Arrange(() => delegateMock(10)).Returns(20);
     
        // ACT
        var foo = new Foo();
        // Assigning the mock to the dependent property.
        foo.FuncDelegate = delegateMock;
        var actual = foo.GetInteger(10);
         
        // ASSERT
        Assert.AreEqual(20, actual);
    }

    Mock delegates documentation
Next Steps