Vadim's Weblog

Never stop learning.

How To: Create a ReSharper File Template.

Posted by Vadim on April 1, 2008

ReSharper is a wonderful tool that makes writing code much easier.  One of the features of this tool is ability to create templates.  With ReSharper you can create Live, Surround With, and File Template.  In this post I want to share with you how to create a File Template for MbUnit Test Fixture.

MbTest Template Selection After completion of this tutorial, you’ll be able to create an MbUnit Fixture by right clicking the test project and adding the test fixture.

 

 

Step 1: Open ReSharper – Options dialog.

Start Visual Studio and from menu bar select ReSharper – Options (you also can get there by pressing Alt-R + O).

ReSharper_Options.

Step 2: Create a new template.

  • In the left panel select File Templates under Templates section.
  • Then select User Templates in Available templates.
  • In this sample we’re going to create C# template, so select Class (C#) in Quick access list (it should be the first item in the list).
  • Last click on Create Template icon ReSharper Create Template icon to see Edit Template dialog box.

Step 3: Create and save MbUnit Test Fixture template.

  • In Name text box enter MbUnit Fixture.  It’s a name that will appear in the menu when you add a new test fixture.
  • In File name prefix text box enter MbTest.  It’s going to be a suggestion for your test file / class.

ReSharper Edit Template General 

ReSharper Template OptionsNext click on everywhere link next to the word Available to see the Template Options dialog box.  Select Only in projects for language radio button and chose C# projects including ASP.NET C#projects) from the drop-down control. Extension for created files leave as cs.

Now we are ready to create actual template.

Enter the code below into Template text.

   1: using MbUnit.Framework;
   2:  
   3: namespace $NAMESPACE$
   4: {
   5:    [TestFixture]
   6:    [FixtureCategory("$CATEGORY$")]
   7:    [Author("Vadim Kreynin", "Vadim@kreynin.com")]
   8:    public class $CLASS$ {$END$}
   9: }

I’m sure that you noticed $NAMESPACE$, $CATEGORY$, and $CLASS$.  These three are ReSharper variables.  You declare a variable by surrounding a word with $ sign.

The reason we created variables because we want to do something with them.

ReSharper has automatically created entries for our variables that looks like this:ReSharper Edit Template Variables

Click on Choose macro link and choose following options in Choose Macro dialog box.

Variable Available macro
$NAMESPACE$ Default namespace for current file
$CATEGORY$ Constant value
$CLASS$ Current file name without extension

Uncheck Editable Occurrence check box for NAMESPACE variable.

You can see that CATEGORY variable has a red link Constant value.  Click on it and enter Category in String value text box of Choose Parameter Value dialog box.

You’re done!  Click on OK button of Edit Template dialog box that should look like this: ReSharper Edit Template

Now you can start using this template.

Next time I’ll show how to create a Live Template for tests, test SetUp(s) and TearDown(s).

 

kick it on DotNetKicks.com

5 Responses to “How To: Create a ReSharper File Template.”

  1. Joel said

    Thanks! Silly question… how do you use a file template? :)

  2. Raffaeu said

    Awesome article. Really useful for person “addicted” to R# like me.
    Thanks a ton!

  3. Gonzalo Calandria said

    Hello, i would like a file template, but that the filename allways end with VO, i mean.. i click on “new from template -> VO template” and the file name be xxxxVO.cs, can i do that?

    • Vadim said

      If I understand correctly, you want “VO” always to be appended to the name you enter in the dialog box. I don’t think it’s possible.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: