Telerik® JustMock™ by Progress

This topic will go through the different ways in which we can set up our test arrangements and assert our tests expectations. Fluent Assertions allow you to easily follow the Arrange Act Assert pattern in a straigtforward way.

Note that JustMock dynamically checks for any assertion mechanism provided by the underlying test framework if such is available (MSTest, XUnit, NUnit, MbUnit, Silverlight) and uses it, rather than using its own MockAssertionException when a mock assertion fails. This functionality extends the JustMock tooling support for different test runners.

In the following examples we will use this sample interface:

Fluent or Explicit Asserts


In order to use the fluent syntax, you need to import the Telerik.JustMock.Helpers namespace in your source file.

Having defined the IFileReader interface we now want to create a mock and to check whether certain expectations are fulfilled.

C# Copy imageCopy
IFileReader fileReader = Mock.Create<IFileReader>();
const string expected = @"C:\JustMock";

// Arrange
fileReader.Arrange( x => x.Path ).Returns( expected ).OccursOnce();

// Act
var actual = fileReader.Path;

Visual Basic Copy imageCopy

' Arrange
Dim fileReader As IFileReader = Mock.Create(Of IFileReader)()

Const expected As String = "C:\JustMock"

fileReader.Arrange(Function(x) x.Path).Returns(expected).OccursOnce()

' Act
Dim actual = fileReader.Path

Here we say that the Path property should be called exactly one time. We do this by using the OccursOnce method. Note that there is no difference between using fileReader.Arrange and Mock.Arrange. The first way is the fluent way of making arrangements but however they are both valid ways of defining your Arrange clauses.

In this example we have also defined that the Initialize method must be called using the MustBeCalled method.

Let's assert our expectations:

C# Copy imageCopy
Assert.AreEqual( expected, actual ); // explicit assert
fileReader.Assert( x => x.Path ); // fluent assert

Visual Basic Copy imageCopy
Assert.AreEqual(expected, actual) ' explicit
fileReader.Assert(Function(x) x.Path) ' fluent

When you use the most general call - fileReader.Assert()Telerik JustMock will actually assert all the setup arrangements marked with either MustBeCalled or Occurs. Note that if there are tests that don’t have these modifiers then you still have to assert them using the explicit assert.

The first explicit assert in this example will call the fileReader.Path property one time and will assert that its value is equal to the expected value.

There is a slight difference between the last two lines.

C# Copy imageCopy
fileReader.Assert( x => x.Path );

Visual Basic Copy imageCopy
fileReader.Assert(Function(x) x.Path)

Will check only the arrangements defined for the fileReader.Path property. In our example Telerik JustMock will verify that the Path property has been called exacly one time.

C# Copy imageCopy

Visual Basic Copy imageCopy

Will check all the arrangements defined in the current test. In our example Telerik JustMock will verify that the Path property has been called exacly one time and that the Initialize method has also been called.


Note that when you use Fluent Asserts only arrangements marked with either MustBeCalled or Occurs will be verified. For other tests you have to use the explicit assert.

See Also