New to Telerik JustMock? Download free 30-day trial

Fluent Mocking

This topic will go through the different ways in which you can set up your test arrangements and assert your test expectations. Fluent Assertions allow you to easily follow the Arrange Act Assert pattern in a straightforward 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:

public interface IFileReader 
    string Path { get; set; } 
    void Initialize(); 
Public Interface IFileReader 
    Property Path() As String 
    Sub Initialize() 
End 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.

using Telerik.JustMock.Helpers; 
Imports Telerik.JustMock.Helpers 

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

IFileReader fileReader = Mock.Create<IFileReader>(); 
const string expected = @"C:\JustMock"; 
// Arrange 
fileReader.Arrange( x => x.Path ).Returns( expected ).OccursOnce(); 
// Act 
var actual = fileReader.Path; 
' 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:

Assert.AreEqual( expected, actual ); // explicit assert 
fileReader.Assert( x => x.Path ); // fluent assert 
Assert.AreEqual(expected, actual) ' explicit 
fileReader.Assert(Function(x) x.Path) ' fluent 

When you use the most general call - fileReader.Assert(), 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.

fileReader.Assert( x => x.Path ) 
fileReader.Assert(Function(x) x.Path) 

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


Will check all the arrangements defined in the current test. In our example JustMock will verify that the Path property has been called exactly 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

In this article
Not finding the help you need? Improve this article