More Supported C# Features in JustMock with R2 2018 [New Release]

In the R2 2018 release, JustMock gains official support for a number of additional C# features. Read on to learn about all the new updates.

With the R2 2018 release of Telerik JustMock, we focused on the C# language and its features. Now, JustMock officially has support for read-only auto properties, auto property initializers, expression-bodied function members, string interpolation, using static, private protected access modifiers, and the in parameter modifier. Let me dive deeper into each of these updates.

Read-Only Auto Properties

As per the Microsoft documentation:

this feature [of auto-properties] enables true language support for creating immutable types and using the more concise and convenient auto-property syntax

Creating a mock of a read-only auto property is as easy as mocking a standard auto property. What needs to be done is create a mock object for the class under test and arrange the read-only auto property as shown in the code snippet bellow:

Auto-Property Initializers

Another supported C# feature is the auto-property initializer. It allows you to easily declare an initial value for an auto property as part of the property declaration. Again, creating a mock for an auto property with inline initializer is as easy as creating a mock for a property.

Expression-Bodied Function Members

The next supported C# feature is the expression-bodied function. It allows you to declare the body of a method or a read-only property as an expression. It is useful when you would like to create the body with a single statement. Here is an example of the syntax of a method with an expression-bodied function:

String Interpolation

String interpolation is the next C# feature in the supported list. It allows you to format a string in a much more simplified way. Here is what the syntax looks like:

string name = $"Name{Name}";

In this scenario, the expression can be directly mocked. Here is an example of how this should be done:

Mock.Arrange(() => $"Name{Name}").Returns("John Doe");

Using Static

using static is another C# feature JustMock supports. The feature allows you to import the static methods of a single class. Repeatedly typing the class name is not needed anymore; static methods can be directly called by name. Here is example of how this feature is used:

using static System.Math;
int max = Max(3,5);

The mock should be done with the already known approach:

Mock.Arrange(() => Max(2, 3)).Returns(expected);

The result of calling Max with first parameter (2) and second parameter (3) will return the value of expected variable. This mock behavior will be applicable only for the test where the arrangement happens and it won't be transferred to other test methods if not explicitly specified. If the scenario is the static method to preserve the mocked behavior for all the tests in the test class the arrangement should be done in a class initialize method.

Private Protected Access Modifier

The private protected access modifier was introduced with C# 7.2 and methods declared with this access modifier can be mocked with JustMock.

Here is what a class using the private protected access modifier looks like:

public class Foo
{
private protected int GetValue()
{
return 42;
}
}

And here is a class that inherits Foo and will make a call to the private protected method, GetValue:

This is everything needed for the mock of a method declared with the private protected access modifier.

in Parameter Modifier

And the last C# feature in this list introduced with C# 7.2 is the in parameter modifier. It is used in a method signature to specify that the argument is passed as a reference but won't be modified by the called method.

The following is a simple class illustrating a method using the in parameter modifier:

Improvements to the JustMock extensions for TFS 2015 and TFS 2018

During the preparation of the release the JustMock team made several improvements to the extensions for TFS 2015 and TFS 2018, and they are already available for download from the Visual Studio Team Services Marketplace.

Check Out the Latest Version and Share Your Feedback

Make sure to try the latest version of JustMock and get back to us with your feedback. It is already available for download in your account.

Rossitza Fakalieva

Rossitza Fakalieva is Manager of the Progress .NET UI & Tools division, which develops the UI for WPF, UI for WinForms, UI for UWP and UI for Xamarin products. She started her career on the Telerik ASP.NET team in 2006 as a support officer and passed through the position of software developer to the position of senior manager. Off work, she enjoys cooking, watching movies and reading books.

Latest Stories
in Your Inbox

I agree to receive email communications from Progress Software or its Partners, containing information about Progress Software’s products. Consent may be withdrawn at any time.

We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.

Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.

Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.