background

Telerik JustMock

Future Mocking

  • Sometimes your code depends on a class instance that you have no control over how it is created. With JustMock mocking such a class instance is a one-line code. This feature is especially handy when you work with third-party libraries, controls or tools.
  • 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.
Mocking and Assert Functionality
  • Future Mocking Overview

    Isolating the tested code from the behavior of newly created class instances somewhere along the chain of execution is a hard task. JustMock allows you to mock the behavior of members of class instances created at a future point in time, enabling you to isolate the tested code from such dependencies. This feature is especially handy when you mock third-party libraries, controls or tools where you have little to no control over how class instances are created.
  • Ignore Instance

    By default, all mocks are tied to an instance and this is the expected behavior. With the IgnoreInstance expectation, you can specify that a particular arrangement should be applied to all instance of the type.

    [TestMethod]
    public void ShouldArrangeReturnForFutureUserDataInstances()
    {
        // Arrange
        var fakeUsed = Mock.Create<UserData>();
        Mock.Arrange(() => fakeUsed.ReturnFive()).IgnoreInstance().Returns(7);
      
        // Assert
        Assert.AreEqual(7, fakeUsed.ReturnFive());
        Assert.AreEqual(7, new UserData().ReturnFive());
    }
  • Mocking New Operator

    Future mocking also allows you to arrange all future instances of a given class to point to a predefined object of your choosing.
    public Foo GetNewFooInstance()
    {
        return new Foo();
    }
     
     
    [TestMethod]
    public void ShouldReturnNewObjectForFutureInstances()
    {
        // ARRANGE - Every new instantiation of the Foo class should return a predefined instance.
        var testObj = new Foo() { MyProp = "Test" };
      
        // Directly arranging the expression to return our predefined object.
        Mock.Arrange(() => new Foo()).Returns(testObj);
     
        // ACT
        var myNewInstance = GetNewFooInstance();
     
        // ASSERT
        Assert.IsNotNull(myNewInstance);
        Assert.IsInstanceOfType(myNewInstance, typeof(Foo));
        // Assert that the returned instance is equal to the predefined.
        Assert.AreEqual("Test", myNewInstance.MyProp);
    }

    Future mocking documentation
Next Steps