Telerik® JustCode™ provides support for Telerik JustMock by automatically generating mocks and arrangements. This makes the writing of JustMock unit tests much easier.
The only thing you need, in order to use this functionality, is the Telerik.JustMock.dll referred to the current project.
Then, creating or arranging a mock through JustCode will automatically include the required usings (Telerik.JustMock; and Telerik.JustMock.Helpers; if fluent syntax is used).
Both, the Create Mock and the Arrange Mock commands are placed inside the Visual Aid menu of JustCode.
Supported: C#, VB.NET
To Create Mocks
Use the Create Mock command. Press Alt+End. From the pop-up menu, select Create Mock. It is available for similar to the following declaration:
Executing the command will result in a creation of a mock with default behavior
To Arrange Mocks
Use the Arrange Mock command. Press Alt+End. From the pop-up menu, select Arrange Mock
Let's assume we have the following Foo class:
Clicking on the Arrange Mock command will prompt a window, containing all mockable members of the instance.
You will notice Telerik® JustCode™ shows non public methods and properties as well. You can also choose between standard arrangements or fluent syntax
The next image describes the functionalities inside the Arrange Mock configuration window:
When you click the Finish button, the following code will be automatically generated for it:
You can use the Arrange Mock command on every mock declaration:
Foo myMock = Mock.Create<Foo>();
var myMock = Mock.Create<Foo>();
Foo myMock; - here the mock is not even created. Its creation will be automatically generated by JustCode.
Foo myMock = new Foo(); or var myMock = new Foo(); - for this initialization, Telerik® JustCode™ will automatically apply partial mocking for the arranged methods.
Expected Arrangement's Behavior
The template for the Arrange Mock command is as follows:
Arranging functions or property getters:
Value type functions/properties are arranged to return the default value for their type:
For string functions/properties the expected return value is String.Empty or "":
Reference type functions/properties will return a new mock of their type:
Bar expectedRefProperty = Mock.Create<Bar>();
Mock.Arrange(() => myMock.RefProperty).Returns(expectedRefProperty);
Bar expectedRefFunction = Mock.Create<Bar>();
Mock.Arrange(() => myMock.RefFunction()).Returns(expectedRefFunction);
Arranging void methods:
Arranging property setters:
At this stage, JustMock allows arranging for property setters only if they are public. The JustCode's Arrange Mock command will generate the following for them:
Value type properties are set the default value for their type:
For string properties the expected set value is String.Empty or "":
Reference type properties will be set to the default of their type: