In this article you will find information about the Debug View usage in Telerik JustMock. It is really useful when it comes to debugging tests and finding where the unexpected behavior comes from.
The article will cover all of the Debug View's functionalities. It will also give some good practices about where and what to look for in order to check the correctness of a certain test.
Debug View can be used only while debugging your test methods. To do so, place a breakpoint at the start of the inspected test and run the debugger. When the breakpoint is hit, open the Watch window and add the Debug View by writing: Telerik.JustMock.DebugView or just DebugView under the Name column. Expanding the Debug View, you will notice there are 4 fields:
At first the fields will be empty. The information in them will be populated step by step, while debugging the test method.
The CurrentState field lists all of the arrangements that have been made during the test execution, all of the invocations and the elevated mocking state. This field will be updated accordingly after every new arrangement or invocation in the test method. You can use the text visualizer to inspect the CurrentState as shown below:
In the Text Visualizer you will be able to see:
- Elevated mocking - Enabled or disabled depending if the JustMock profiler is turned on or off.
- Arrangements and expectations - Here you will find full list of all the arrangements made so far along with their expectations.
- Invocations - Shows all invocations with their arguments types and values. Another thing you can see, is how many times certain invocation has been executed.
Tracing your test method gives more details about the intercepted invocations order. The JustMock Debug View will gather information about all intercepted calls. It will show their stack traces, matching arguments, the chosen arrangement, calls made so far and the return value (for functions). Another thing when trace is enabled is that, the debugger will print the tracing log in the Output window.
To enable the tracing mode inside the Debug View, you must set the IsTraceEnabled field to true at the beginning of the test method. This can be achieved by putting a breakpoint there and editing the value inside the Watch window.
Another option is to add:
DebugView.IsTraceEnabled = true; as a first line of the test method. This will automatically enable the tracing option in debug mode.
While tracing, you can operate with the rest two fields from the Debug View: the FullTrace and the LastTrace.
- FullTrace - Gives information about all the intercepted invocations in the test method.
- LastTrace - Gives information only about the last intercepted invocation in the test execution.
Once again, you can use the text visualizer to inspect the information in the fields:
In the text visualizer you will be able to see the following information about each intercepted call: * Stack trace - The full stack trace of the invocation.
- Matching arrangements on the particular call - All arrangements made for that member.
- Chosen arrangement - The applied arrangement in the test method.
- Number of calls - The number of calls made to that member.
- Return value (for functions only) - The return value of the function.
As the logs can be rather long, you can copy them from the Visual Studio's Text Visualizer and paste them in another source code editor (Notepad++ for example). Then, changing the language to Python will provide the possibility of collapsing or expanding certain groups inside the log. Further, you will also get an appropriate syntax highlighting:
JustMock extension for Visual Studio 2017 (and above) makes mock debugging even more convenient by adding tool window for visualizing trace messages and arranged method mocks. It can be opened from main menu View > Other Windows > JustMock Debug Window or extension menu - JustMock > Debug Window > Show. There is also an option to enable or disable debug window by using the corresponding command in the extension menu.
Enabling or disabling the debug window will be persisted and will be applied not only on the current instance of Visual Studio but also on all future ones.
The debug window consist of two panes which are active only while debugging:
- Debug Trace
This pane shows the same content as Full Trace, but it does not required any explicit activation. The sample output produced by the sample unit test looks as following:
- Mock Repository
The pane shows basic information about particular method mock like name, declaring type and its invocations. The test method sample has the following repository view: