Tips and Tricks

Resharper templates for unit testing with NUnit

If you’ve ever done test driven development for (TDD) over an extended period of time, you’ll probably agree that writing the same boilerplate test-code over and over can be a hassle. I’m talking specifically about structures like the following:

[TestMethod]
public void SetUp(){ ... }

[TestMethod]
public void Should_Do_Something_Or_Other(){ ... }

[TestCase("input")]
public void Should_Get_Some_Info(string targetPerson){ ... }

 

Writing a large number of tests quickly is perhaps the primary defining characteristic of a TDD process, so being able to generate code like the above quickly is an obvious advantage. If you use Resharper, you can add your own code templates to help you achieve this.

Below you’ll find three templates that I’ve been using for some time now, along with a description of how you can set them up in Visual Studio in under a minute.

Resharper Live Template Editor

Resharper Live Template Editor

Continue reading

How to switch desktops when Hyper V steals your focus

I currently run Windows 10 as my main OS, yet I often run Windows 8.1 in a virtual machine under Hyper V to work on a project no longer supported in Windows 10. This is quite practical now that multiple desktops are supported out of the box: I typically run Windows 8.1 in full screen on a separate desktop, so it does not interfere with my other desktop(s).

Tip: In Windows 10, press the Windows key + Ctrl + D to create and switch to a new desktop. Hold down Ctrl and the Windows button while pressing the right or left arrow to switch between desktops. You can also use win + Tab, and click the desktop-icons at the bottom of the screen to switch between them.

One thing I found annoying was the difficulty of switching away from Hyper V and Windows 8.1, since the virtual machine will typically steal the focus. I’ve found a relatively simple solution now though: When the focus is in Hyper V, Ctrl + Alt + Left arrow will move it out and into the outer context – in my case away from my virtual Windows 8.1 machine, and back out to Windows 10. This shortcut is probably not new, but I was unaware of it until recently.

In short:

  • To move from a regular Windows 10 desktop and right into a full screen windows 8.1 desktop, I’ll use: Ctrl + Windows + arrow key.
  • To move back out from Windows 8.1, I’ll first press Ctrl + Alt + left arrow, followed by Ctrl + Windows + arrow key.

This may sound a little awkward, but I’ve found it surprisingly easy to get used to, and as far as I can tell, this appears to be the easiest way to escape from a VM.

Replace a string with newline in emacs

I sometimes find myself wanting to replace certain strings in a text file with a new-line character. Not with “\n” or “\r\n” mind you, but an actual break to make the text more readable in the editor. Take for example a random stack trace from Visual Studio. I sometimes copy these out to another simpler text editor to examine them closer. The problem is that when pasted, they will appear as a single line with “\r\n” instead of actual line breaks:

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)\r\n at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid clsid)\r\n at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)\r\n at System.Windows.Forms.AxHost.CreateInstance at System.Windows.Forms.AxHost.GetOcxCreate()\r\n at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)\r\n at System.Windows.Forms.AxHost.CreateHandle()\r\n at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n at System.Windows.Forms.AxHost.EndInit()\r\n at MyApplication.frmPrint.InitializeComponent() in (...)

If your text editor breaks lines at the edge of the window, it might look more like this:

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& 
clsid, Object punkOuter, Int32 context, Guid& iid)\r\n at 
System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid 
clsid)\r\n at System.Windows.Forms.AxHost.CreateInstanceCore(Guid 
clsid)\r\n atSystem.Windows.Forms.AxHost.CreateInstance at 
System.Windows.Forms.AxHost.GetOcxCreate()\r\n at 
System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)\r\n at 
System.Windows.Forms.AxHost.CreateHandle()\r\n at 
System.Windows.Forms.Control.CreateControl(Boolean 
fIgnoreVisible)\r\n at System.Windows.Forms.Control.CreateControl(Boolean 
fIgnoreVisible)\r\n at System.Windows.Forms.AxHost.EndInit()\r\n at 
MyApplication.frmPrint.InitializeComponent() in (...)

..which may be better, but still not very readable.

Most text-editors have some form of replace-all utility built in, but it’s not always easy to replace “\r\n” with an actual line shift (try it!). In Emacs, there is a nice way to handle this:

Use M-x Replace-String to prompt for the string to replace. Enter “\r\n” in the mini-buffer. Press enter, and it will prompt for the string to replace it with. At this point, use C-q C-j to input a new-line (C-j) as quoted text (C-q), and press enter again.

The result will be a much more readable stack trace:

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid clsid)
at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)
at System.Windows.Forms.AxHost.CreateInstance at System.Windows.Forms.AxHost.GetOcxCreate()
at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
at System.Windows.Forms.AxHost.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.AxHost.EndInit()
at MyApplication.frmPrint.InitializeComponent() in (...)

Viewing Linqpad scripts in VS2013

I use LINQPad all the time at, both for experimenting with C#, and for building and testing Linq queries before I import them in our projects.

As an example, I recently wrote a few scripts for fetching some data from a database, and reformatting it into JSON data which we will use as dummy-data for testing. This will not be a part of our finished project, but it’s still nice to have the script visible in Visual Studio, even if I only run it through LINQPad .

The following are a couple of tips I’ve found useful when working with .linq-files in or from within Visual Studio.

Continue reading