;

Creating a visual studio Arrange-Act-Assert snippet

Posted : Thursday, 06 December 2012 10:44:00

First things first I’m big on unit testing, I love it! From the time I read this book The Art of Unit Testing I was sold. If anyone reading this wants to know more about unit testing this book is the place to start.

Anyway, I find myself writing a lot of tests (generally using MsTest but that’s purely out of laziness) and while the standard snippet that comes with Visual Studio is ok, its not quite what I want. After reading the above book I like to structure my tests using the skeleton below:

[TestMethod]

public void Method_Scenario_Expectation()

{

    //Arrange

    //Act

    //Assert

}

Creating tests using the built in Visual Studio snippet is better than nothing but then I have to make the same alterations every time, namely changing the method name and adding the Arrange,Act,Assert comments. Recently I decided to try and create a snippet to do this for me and it was alarmingly easy. I found a few blog posts on the subject and its quite a simple process so rather than go over the whole thing – I’ve just included the snippet below.

<?xml version="1.0" encoding="utf-8"?>

<CodeSnippets

    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.0.0">

    <Header>

      <Title>MsTest unit test snippet</Title>

      <Author>Dave Lowe</Author>

      <Description>Adds a unit test stub method for MsTest</Description>

      <Shortcut>aaa</Shortcut>

    </Header>

    <Snippet>

      <Declarations>

        <Literal Editable="true">

          <ID>Method</ID>

          <ToolTip>name of method under test</ToolTip>

          <Default>MethodName</Default>

          <Type>string</Type>

        </Literal>

        <Literal Editable="true">

          <ID>Scenario</ID>

          <ToolTip>scenario under test</ToolTip>

          <Default>Scenario</Default>

          <Type>string</Type>

        </Literal>

        <Literal Editable="true">

          <ID>Expectation</ID>

          <ToolTip>expected behaviour</ToolTip>

          <Default>Expectation</Default>

          <Type>string</Type>

        </Literal>

      </Declarations>

      <Code Language="CSharp">

        <![CDATA[

        [TestMethod]

        public void $Method$_$Scenario$_$Expectation$(){

          //Arrange

          //Act

          //Assert

        }]]>

      </Code>

    </Snippet>

  </CodeSnippet>

</CodeSnippets>

As you can see its pretty straightforward, there are three literal regions which you can tab between. I assigned the shortcut “aaa” (for Arrange Act Assert) to make it nice and easy to use. The last step was to add the folder containing this file to Visual Studio (Tools –> Code Snippet Manager –> Add…), back in any C# class file typing “aaa” and hitting tab twice generated the snippets desired – bingo! Incidentally there is tool to do this for you but you gotta love getting into the XML right?

Except…

I wrote the snippet on the train on my laptop and the user account I was running under didn’t have ReSharper installed normally any account I use would have but I’ve just started at a new clients and only just put the laptop on their domain so I haven’t had time to set everything up yet. When I got home I was working on my Desktop PC, added the snippet but to my dismay nothing happened when I tried to use my new snippet – after a bit of research it turns out ReSharper has some very similar functionality that goes under the term of “Live Templates” and they don’t play so well together – I’ve used ReSharper for over a year now and I hadn’t seen this before (which makes me wonder how much other cool stuff I don’t know about….?). I came across this blog post where the author talks about the same issue and highlights how to use the ReSharper live template feature – whats really cool is in the actual snippet code (the CDATA section in the XML above) can be pasted into the ReSharper live template designer.

image

I did this and it just worked straight away. Yet another reason to love ReSharper

  • (This will not appear on the site)