Progress Telerik JustMock

Future Mocking

Nasa, Visa, Microsoft Fox, Samsung, IBM World Bank Group, Volvo
  • 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.

    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();
    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.IsInstanceOfType(myNewInstance, typeof(Foo));
        // Assert that the returned instance is equal to the predefined.
        Assert.AreEqual("Test", myNewInstance.MyProp);

    Future mocking documentation

Next Steps

See Plans & Pricing

Purchase individual products or any of the bundles

Download Free Trial

With dedicated technical support.