Mock is the main class in the Telerik JustMock framework. Mock is used to create instance and static mocks,
arrange and verify behavior.
We will use the following interface as system under test for some of the examples in this article:
Create Instance Mocks
To create instance mocks you need to use the Mock.Create method or its generic version Mock.Create<T>.
With this you create a fake object that replaces the real one in your test.
Additionally you can specify initializing arguments to be passed to the constructor and
If you don't have a default constructor in the type you are creating instance of or want to use the non-default constructor, you can use another override of the Mock.Create
method which takes a LINQ expression. The following example demonstrates that:
The Arrange method is used to change the behavior of method or property calls on a mock.
It is used in conjunction with one or more of the supported behaviors described in this section:
CallOriginal() - use the original method implementation.
DoInstead() - execute a custom code when the method is called.
Initialize() - sets up all framework methods.
DoNothing() - ignore the call. This method is used only for readability and is applicable only for void methods.
MustBeCalled() - mark the method assert that it is called during the executing of the test.
Raise() - raise mocked event.
Raises() - raise an event once the method is called.
Returns() - use with a non-void method to return a custom value.
Throws() - throw an exception once the method is called.
Here is an example of how to arrange a method call to return a custom specified value.
If you want to mock a property set, instead of using the Arrange method you should use the ArrangeSet method.
The following example demonstrates how to arrange the throwing of an exception once a property is set to a certain value.
Arrange - using expressions with dynamic values
The Arrange method also allows you to use dynamic values in the argument expression.
This gives you granular control over an arrangement utilizing a lambda expression. Let's have a look at the following scenario:
Here we have a BookService which we use to get specific books form the repository. The
IBookRepository has only one method - GetWhere which is used to return a Book
specified by the lambda expression that comes as a parameter. Take a look at the GetSingleBook method of the BookService class
which uses a lambda expression to get a book with a specific id.
In the following test we use lambda expression in the Arrange phase:
We specify that when the repository GetWhere method is called with an id of 1 the returned book should be a specific book.
Then we act - we execute the GetSingleBook method of the BookService and we assert that the expected book is returned.
Note that the expressions you pass must match value types. For example, book.Id > 0 will not be called by service.GetSingleBook(1) because the call to
GetSingleBook(id) will only be arranged if you pass book.Id == id in the arrange phase.
Auto arrange virtual properties set from constructor arguments
As you saw in the first section above, when you use Mock.Create, you can specify initializing arguments to be passed to the constructor of the created object.
When the constructor sets the values of virtual properties contained in the type you are mocking you can use Mock.Create in the same way.
The result will be that the values of the virtual properties will be automatically arranged. Let's see an example demonstrating this feature:
Assert - Verify Behavior
After you arrange the behavior of certain method/property calls and act then you need to verify the returned results or the behavior in general.
You do this with the Mock.Assert method.
Let's assert that an arrange method is actually called.
Even if you don't arrange the method invocation you can still assert whether the method is called.
You can also assert property get calls in the same way as method calls.
To assert a property set instead of Mock.Assert you need to use Mock.AssertSet. To demonstrate
the use of Mock.AssertSet we will use one of the behaviors mentioned earlier in this topic, namely MustBeCalled.
We will verify that the property was actually set during the test run.
In this example we verify that the Bar property has been set to 0, not that the property has been set to something.
To verify that the property has been set at all, we can use the matcher Arg.Matches.
Let's finish this topic with one little more complex example. You may have a case where you return a list of values. The next example demonstrates how to verify the number
of returned items and asserts that a specific method is called.
For this example we will be using the following IFooRepository: