• 10 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 8/6/19

Modify data values directly

It’s time to get back to the T_WebPageTask.CheckCreation unit test that has failed. In the last chapter, we were left with a breakpoint defined prior to the call to Assert. It has failed because the task starting date is not valid:

Invalid Start Date
Invalid Start Date

It looks as if it was never initialized. All we need to do is go into the TodoTask constructor and add the code for storing today’s date as the start-point for the task:

Adding the missing initialization code
Adding the missing initialization code

In this way, all the derived classes will benefit from the fix.

When the test is rerun to validate the correction, the final validation fails:

Failure of final Assert
Failure of final Assert

The AbsolutePath property isn't what we expected. This time, let's correct the unit test rather than the code for the class, which is based on the value of an incorrect property.

How do we find the right one?

We display the task and then its property URL to see all the properties available, together with their values:

Value of AbsoluteUri property in QuickWatch
Value of AbsoluteUri property in QuickWatch

We can see at once that we need to base it on AbsoluteUri rather than on AbsolutePath:

          

And now the test passes!

                                     

We passed the test, but is it complete? The parameters passed to construct a WebPageTask seem to all be tested. Except that managed enumerations mean that the first value of the enumeration is also the C# compiler default one used when the variable is not explicitly initialized. Was changing the category value during the WebPageTask creation within the test taken into account? Was the value Science (first value in the enumeration) put there explicitly by the constructor code or implicitly by the compiler?

Default enumeration value
Default enumeration value

The best solution is to modify the unit test accordingly. However, it’s possible to see and modify the value of variables. So let’s look at how to modify the value of a parameter by rerunning the debugger with a breakpoint prior to constructing the WebPageTask.

It’s very simple! All you have to do is view the value using any of the previously-quoted methods, and then click on the value itself to change it:

Changing a value using auto-complete
Changing a value using auto-complete

The text entry box offers the same auto-complete function as the editor. Once the value has been selected, press ENTER to confirm.

The new value appears everywhere (and even in red to indicate it has been modified during the latest execution):

The modified value appears in red in all the panels
The modified value appears in red in all the panels

If the constructor code is now run using the new value for the category using Step Over (F10),  you can see that the category for the WebPageTask created is not the right one. Let's revert to the default enumeration value:

And now the unit test’s second assertion fails. :(

To understand where the problem is, let's return to before the execution of the creation of the WebPageTask using Set Next Statement:

Returning to before the creation of the WebPageTask
Returning to before the creation of the WebPageTask

And go down into the constructor using Step Into (F11). Only the URL is stored at this level:

Webpagetask constructor
Webpagetask constructor

The name and category are handled in the code for the base class, which is accessed using Step Into (F11) again:

Readtask constructor
Readtask constructor

In fact, this code does not take the contentCategory parameter into account for giving the value of the corresponding Category property.

All we have to do is write the code to fix the problem. We can even do it while we’re debugging!

Editing the code while you’re debugging
Editing the code while debugging

This involves the Edit and Continue function: once the modification has been made, continue normal execution using Step Over (F10) or Step Out (Shift  +  F11). However, there is a simpler way of returning to the unit test using the Call Stack panel via a right-click Run To Cursor!

Using Run To Cursor from the call stack
Using Run To Cursor from the call stack

We are back in the test, just prior to the return from the constructor:

Now Step Over (F10) to see the result:

Locals status after the fix
Locals status after the fix

Let's recap!

You have seen how, in addition to viewing your data, the Visual Studio debugger makes it possible to modify its values and even the code without re-compiling during an investigation. 😁

Example of certificate of achievement
Example of certificate of achievement