New to Telerik JustMock? Download free 30-day trial

Environment

Product Version
Progress® Telerik® JustMock 2021.2.511.1

Description

While using Progress® Telerik® JustMock, you might encounter an unexpected behavior where an assertion on a mock object incorrectly includes assertions for another mock object that is used as a return value in one of its arrangements. This can lead to confusion when assertions fail for the wrong reasons.

This knowledge base article also answers the following questions:

  • How can I isolate mock assertions in JustMock?
  • What is the correct way to set up mock dependencies in JustMock?
  • How do I ensure accurate assertion behavior for mock objects in JustMock?

Solution

The behavior observed is due to JustMock's feature that automatically creates dependencies between mocks when one mock is returned as a result of another mock's arrangement. This can inadvertently cause assertions to validate all related mocks, not just the one explicitly specified.

To isolate assertions to the intended mock object without affecting its dependencies, utilize the Returns overload with a Func<TResult> parameter type. This approach prevents JustMock from creating automatic dependencies between the mocks.

Example

Consider the scenario where you have a mock SqlConnection object and a mock SqlTransaction object:

// Arrange
SqlConnection sqlConnection = Mock.Create<SqlConnection>();
SqlTransaction sqlTransaction = Mock.Create<SqlTransaction>();

Mock.Arrange(() => new SqlConnection()).Returns(sqlConnection);
Mock.Arrange(() => sqlConnection.BeginTransaction()).Returns(sqlTransaction);

// Assert
Mock.Assert(sqlConnection, "Problem in Connection"); 

In the sample code when asserting sqlConnection will automatically assert sqlTransaction. By changing the arrangement to use a Returns overload with Func<TResult> the assertion against sqlConnection will not inadvertently include sqlTransaction. Here is the code:

Mock.Arrange(() => sqlConnection.BeginTransaction()).Returns(() => sqlTransaction);

See Also

In this article