ReSharper Recipes. File templates.

Now I’ll redeem a promise about which I’ve told in the previous article. I’ll tell you how to reduce your working hours which you spend on writing tests.

If you can create new classes with the help of alt +ins in the Solution Explorer, why don’t we create text classes with all we need at once?

It is really possible, but in a more trivial way. So, let’s start.

Again we need a draft, but now we can use it with different names and with all inclusions of «using ». So, let’s use this code for our experiment.

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests {
    public class Class1 {
        public void TestMethod() {  }

After “ReSharper > Live Templates…” we see a window where we can create code’s drafts. Then move to the tab “File Templates”. Open the node “User Templates”, invoke the context menu for it or  “<No Languages>” and choose “New template”.

After it in the main part of the studio we can see a tab, where we are going to design a draft.

Copy the code which we want to see as a draft and paste it to a window, name the draft “Test” and design variable parts. So, in the end you’ll see something like this:

Now we have three variables, two of which you can fill automatically. They are Namespace and Classname. For realization of these things you need to choose proper macros. Click on  “Choose macro” and choose:

  • For Namespace it will be “Default namespace for current file”;
  • For Classname –  “Current file name without extension”.

Take away check marks opposite the same variables, in order to while creating a file ReSharper asked us to enter only the test names. And don’t forget to mark where the cursor should be after creating of a file with the help of the variable $END$.

Save all what we have. However it’s not everything what we need to add automatically the point with test class in context menu for creating files. You should do some more things. For the last version of the ReSharper the process is a bit different from the previous.

  • ReSharper before the version 4.5

Follow the points in the menu “ReSharper > New from Template > More…”. You should move from the tab with a creating of a draft necessarily, otherwise the point in the menu “New from Template” will be empty.

  • ReSharper version 5.x

Invoke the context menu with a click on an any project and move in menu’s points “Add > New from Template > More…”

In the shown up window find our draft, put a check mark opposite “Add to quicklist”. It will automatically show that it is for projects on С#.

Click “OK” and now you can create text classes from the project reviewer!

You can delete drafts from the context menu on the screen Template Explorer ( second picture). For it you should choose your draft from the right panel and click on a red cross.

Thus you can make other models of classes which you use regularly in your project. I hope it will help you in the future to save a lot of time and nerves. =)


Tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>