background

Telerik JustMock

Mock Local Functions

  • JustMock makes isolating your unit test from a dependency to a local function an easy task, allowing you to mock it. In most cases, mocking local functions is similar to mocking class methods.
  • 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 Local Functions Overview

    A local function is a private function whose scope is limited to the scope of the function in which it is created. The local function can be called only within the container member and with this—a dependency to the container member.

    JustMock makes isolating your unit test from the local function an easy task, allowing you to mock it. In most cases, mocking local functions is similar to mocking class methods.

    class Foo
    {
        public int GetResult()
        {
            return 100 + GetLocal();
            int GetLocal ()
            {
                return 42;
            }
        }
    }
     
    ...
     
    [TestClass]
    public class MockLocalFunctions
    {
        [TestMethod]
        public void BasicUsage()
        {
            //Arrange
            var sut = Mock.Create<Foo>(Behavior.CallOriginal);
            Mock.Local.Function.Arrange<int>(sut, "GetResult", "GetLocal").DoNothing();
      
            //Act
            var result = sut. GetResult();
      
            //Assert
            Assert.AreEqual(100, result);
        }
    }

    Mock local functions documentation
Next Steps