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:
This is the class that will be tested:
public class ReadOnlyAutoProperty
{
public string FullName { get; }
public ReadOnlyAutoProperty()
{
this.FullName = "Mihail Vladov";
}
}
And this is how the create and arrange should be done.
var foo = Mock.Create<ReadOnlyAutoProperty>();
Mock.Arrange(() => foo.FullName).Returns("John Doe");
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.
This is the property that will be tested:
public class ReadOnlyAutoProperty
{
public string Name { get; } = "Mihail Vladov";
}
And this is how the create, arrange and assert should be done.
var foo = Mock.Create<ReadOnlyAutoProperty>();
Mock.Arrange(() => foo.Name).Returns("John Doe");
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:
public string GetFullName()=> string.Format("{0} {1}", FirstName, LastName);
And here is an example of the syntax of a read-only property with an expression-bodied function:
public string FullName => string.Format("{0} {1}", FirstName, LastName);
JustMock allows you to create and arrange the mock for an expression-bodied function the same way as a block bodied function. Here is the class that will be tested in the following examples:
public class ClientInfo
{
public string FullName => string.Format("{0} {1}", "Mihail", "Vladov");
public string GetFullName() => string.Format("{0} {1}", "Mihail", "Vladov");
}
And here is how the method with the expression-bodied function should be arranged:
var mockObj = Mock.Create<ClientInfo>();
Mock.Arrange(()=> mockObj. FullName).Returns("John Doe");
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
:
public class Bar : Foo
{
public int GetAddValue()
{
return GetValue() + 15;
}
}
The arrange for the private protected
method, GetValue
should be done with the help of the NonPublic
expectation like so:
var mockObj = Mock.Create<Bar>(Behavior.CallOriginal);
Mock.NonPublic.Arrange<int>(mockObj, "GetValue").Returns(32);
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:
public class ClassUnderTest
{
private int GetResult(in int value1, in int value2)
{
return value1 + value2;
}
}
Creating a mock and making the arrange remains the same as with a normal private method with ref
keyword. Of course, the NonPublic
expectation should be used for making the arrange like this:
var mockObj = Mock.Create<ClassUnderTest>(Behavior.CallOriginal);
Mock.NonPublic.Arrange<int>(mockObj, "GetResult", ArgExpr.Ref(ArgExpr.IsAny<int>()), ArgExpr.Ref(ArgExpr.IsAny<int>())).Returns(42);
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.
Not a JustMock customer? Feel free to download a free 30 day trial.