You find the PDF version: here
The sample project: here
Introduction
The new IDE features in Visual Studio 2013 are quite nice and make our beloved IDE again better. Below you will find examples how you configure and use these new features in a day to day (developer) business.Requirements
To follow the samples below, you need a working installation of Visual Studio 2013 on your machine (any edition will do).Setup
Setup a sample project or download the final sample from: hereAuto complete brackets
As you might have figured out during the sample project setup, if you type some kind of bracket in Visual Studio 2013, you will see that the editor now auto closes these brackets. This feature is available in all code or markup editors.Switch color schemes
Select the color scheme of your choice for your Visual Studio 2013 IDE.· Goto: Tools -> Options -> Environment -> General and select the theme of your choice
Quick Launch Box
Visual Studio offers
many configuration options and no one can really remember where to find which
option, but you might remember the search term, so for example ‘color’ to
change the Visual Studio theme, or ‚line numbers‘ to enable the line numbers in
code files. This is where the quick launch box comes in handy. Quick Launch
search results appear in four categories: Most Recently Used, Menus, Options,
and Open Documents, along with the number of items in the category. Let’s try
it.
· Enter: Color (shows color scheme)
=> click on the result, this will bring you directly to the options to configure the color
·
· Enter: Line numbers
=> click on the result, this will bring you directly to the options to enable line numbers in the code editor
· Enter: blank line
=> click on the result, this will bring you directly to the options to configure the “blank line” feature (which stops you from copying a blank line to the clipboard – which happens some times to me when I am too fast with my fingers).
Search Improvements
Visual studio 2013
tries to support developers much more as before, when it comes to searching and
filtering items. You will find new search boxes on several new places. Let’s
have a closer look at them.
Searchable error list
· Build the sample app· You will see three errors filter the errors
· Type a ; in the search box
=> the result is filtered to the two errors
Regular Expression Search
· Open the search dialog (type CTRL+F)· Type C[ao] into the search box
· Enable the regular expression search
=> All script elements starting with Ca or Co are selected.
Solution explorer search
Solution Explorer Enhancements
The solution explorer
was always the one window which is always visible on my screen (or my second
screen). Very nice enhancements can be found here.
2. Pending Changes and Open Files Filter
3. Preview selected items
4. Move somewhere else in sol. Explorer, move again to the .cs file
5. Pin it using the pin
6. Scope to this
7. Open Is Called by on AddNumbers
Navigating Back and Forward
Sometimes I close a
code window too early, so it might be great if I could bring it back.
· Close Calc.cs
· Press CTRL-
Yes, this is again a
nice feature.
In every project there
are a few very important classes. Man, it would be so cool if I could ‘pin’
them so they are not moving away in the open screen tab list.
· Search for pinned tabs
· Pin one of the two code files
Code Editor Features
Code editing is
important for a developer. Again, some small, but important enhancements can be
found in this category.
2. Uncomment lines
3. See the red line and the yellow ones for changes
4. Click on the red line
5. Fix the error (red line disappears)
6. Save the changes
7. Select for example a method and CTRL M H (hide selected code) and CTRL M U (unhide)
8. Show that TAB key hops out of brace
9. Press CTRL +,
10. Search for Console and have a look at the orange lines
11. Open the Scroll Bar options page (Tools, Options, Text Editor, All Languages or a specific language, or type scroll bar in the Quick Launch window) and switch mode
12. Now click ALT+F12 on AddNumbers
13. Correct the logical coding mistake (minus).
Debugger Enhancements
Remember the time,
when you were able to change the code during debugging? When 64 bit apps
arrived this feature was gone. Now, it’s back!
2. Set a breakpoint on var result = Calcs.AddNumbers(5,7);
3. Run the app
4. Make some change and explain that this is allowed now.
5. Step over the method call
6. In the Autos window, note that the return value is displayed and include the return value icon. (To open the Autos window, choose Debug, Windows, Autos, or press Ctrl + Alt + V, A)
7. Type: $ReturnValue in the Immediate window or a watch window (Debug, Windows, Watch) after you have stepped over or out of the method call. To open the Immediate window, choose Debug, Windows, Immediate (keyboard: Ctrl + Alt + I).
8. ReturnValue does not work in phone apps
9. Try the Code Map
Unit testing
A friend of mine said
once to me: „Everyone is talking about unit tests, but no one is actually doing
them“. Well, maybe that’s true, but you might take a closer look into it, you
might start using it. If you already used it and miss the
right-mouse-button-click “Generate Unit Test” popup menu, I have good news for
you (thank you goes to the ALM team) – you are free to bring it back.
Also, VS 2013 is now
open to other unit test frameworks, that’s why I used xUnit in my sample below.
If you like the built in unit test framework more, go ahead with it, it works
the same way.
1. Bring back on RMB on a method declaration.
2. Install from Tools -> Extensions and Updates
3.
4. Install xUnit the same way
5. Press RMB Create Unit test on AddNumbers
6. Implement the unit test
7. Run the unit test in the standard manner by right- clicking on them in the code
editor or from the much improved Test Explorer window (Main menu Test ->
Windows ->Test Explorer).
Fakes Framework
In Test Driven
Development isolating dependencies, mocking objects and faking objects is the
daily business. There are also great 3rd party frameworks out there,
which allow us to isolate dependencies without having to use any hand written
mocks or fakes, but we also have everything we need built into the IDE waiting
for you and me to simply use it.
The Microsoft Fakes
framework was introduced in Visual Studio 2012 Ultimate, but I would like to
give you a quick run-down on how to use Fakes, Shims and Stubs to build a
robust test harness for your own applications. To get started we need a new
class, which is not so easy to test with a simple unit test.
namespace VSEnhancements { public interface ICustomerRepository { Customer GetById(int id); bool Exists(string email); void Delete(); } public class Customer { public string Firstname { get; set; } public string Lastname { get; set; } } public class CustomerService { private readonly ICustomerRepository _repository; public CustomerService(ICustomerRepository repository) { _repository = repository; } public bool DoesCustomerExist(string email) { return _repository.Exists(email); } } }· Click RMB on DoesCustomerExist and Generate Unit Test
Implement stubs
Stubs, ok wht is it? Let Martin Fowler explain it in his own words:
“Stubs provide canned answers to calls made during the test, usually not
responding at all to anything outside what's programmed in for the test. Stubs
may also record information about calls, such as an email gateway stub that
remembers the messages it 'sent', or maybe only how many messages it 'sent'.”
//Stubs test [Fact()] public void DoesCustomerExistTest() { var stub = new StubICustomerRepository { ExistsString = (email) => { return true; } }; var service = new CustomerService(stub); Assert.True(service.DoesCustomerExist("test@test.com")); }
[Fact()] public void ClosureShouldSetValueToExpected() { //this will get set inside our stub string closureToTest = ""; var stub = new StubICustomerRepository { ExistsString = (email) => { closureToTest = "test"; //in reality set to some variable you want to test return true; } }; var doesntMatter = stub.ExistsString("ignore@ignore.com"); const string expected = "test"; Assert.True(closureToTest == expected); }
Implement Shims
Enabling Fakes is
about creating a fake assembly that allows you to use (fake) types that replace
.NET types with delegates. This is done by right clicking on the assembly and
adding the fake assembly. In this sample case it is the System.dll, which wraps
the mscorlib.dll that contains the DateTime object, which I would like to
change to return always the 1st January of the year 2012.
After that I add a
shim for the AddNumbers method to return another result (a multiplication).
· Implement shim for DateTime
//Shims [Fact()] public void Should_Override_DateTime_Now() { using (ShimsContext.Create()) { ShimDateTime.NowGet = () => new DateTime(2012, 1, 1, 12, 00, 00); Assert.True(DateTime.Now == new DateTime(2012, 1, 1, 12, 00, 00)); } }· Implement shim for AddNumbers
· Open CalcsTests.cs
[Fact()] public void AddNumbersTest() { const int expected = -2; int actual = Calcs.AddNumbers(5,7); Assert.Equal(expected, actual); } [Fact()] public void Should_OverrideAddNumbers() { using (ShimsContext.Create()) { ShimCalcs.AddNumbersInt32Int32 = (x, y) => x*y; const int expected = -2; int actual = Calcs.AddNumbers(5, 7); Assert.Equal(35, actual); } } }· Run unit tests
· Just for fun check the CodeLens feature on the AddNumbers method
More Info
· Keyboard shortcuts:http://kbmode.com/windows/microsoft-visual-studio-2013-keyboard-shortcuts/