Visual Studio Code Snippets

Over the years I’ve developed a number of snippets to help me write unit tests using the MSpec framework. After numerous requests from envious co-workers (actually one request from a mildly curious co-worker), I’ve finally open sourced them on Github:

https://github.com/ColinOrr/vstudio-snippets

Installation

Run the following commands to clone the snippets from my Github repository:

cd "Documents\Visual Studio 2013\Code Snippets\Visual C#"
git clone https://github.com/ColinOrr/vstudio-snippets "My Code Snippets"

Visual Studio watches this folder and automatically loads the snippets, so no need for a restart!

test

test demo

The built-in test snippet generates an MSTest method (yuck!). Since we use MSpec, I’ve replaced this snippet with an MSpec test container.

lam

lam demo

MSpec uses lambdas to link specification statements with real code. I find them awkward to type, especially since Visual Studio formatting goes haywire and messes up the indentation. The lam snippet fixes the indentation and drops you straight into the lambda body, ready to start writing test code.

argnull

argnull demo

For production code, you can use the argnull snippet to speed up your argument guards. This snippet uses the same parameter for both the guard and the error message, so you only have to type the argument once. You get an extra boost since intellisense kicks in to auto-complete the argument name.

testnull

testnull demo

The ying to argnull’s yang, testnull generates a test to exercise your argument null check.

Note: this test inherits from a base class called Context which isn’t standard MSpec. I always create a context class for each test file to share context and setup between multiple tests.

guid

guid demo

It can be handy to seed well known GUIDs in order to test retrieval by ID. But no one ever remembers how may digits are in the stupid things - guid snippet to the rescue!

Note: you can provide two sample digits in case you make it all the way up to 10.

any

any demo

More finger-gymnastics are required when using NSubstitute’s catch all argument matcher, the any snippet makes life easier.

Bonus Boxcar Script

boxcar demo

MSpec expects your specification statements to_be_in_boxcar_case. This is tedious to type compared with using the space key. I’ve included a AutoHotKey script to convert anything surrounded in double quotes on the current line into the correct format.

  1. Install AutoHotKey
  2. Double-click on Boxcar.ahk in your snippets folder
  3. Create a shortcut to this file into your startup folder

Once the script is running you can press Right-Alt + Return to format the current line.