Skip to content →

Quick ReSharper test template tips

I just want to share a quick tips about writing tests using ReSharper in Visual Studio. It helps me to speed up tests creations and keep them on the same format.

ReSharper has the functionality to define templates. There are a lot of them out of the box but you can also create your own custom ones.

Go to ReSharper->Template explorer. You can administrate your templates by 3 different categories:

  • Live templates
  • Surround templates
  • File templates

Their pretty much self explanatory. Live templates for creation of code in editor, surround templates for surrounding preexisting code in the editor(with a loop for example), and file templates for creation of new files.

We want to create 2 new live templates. One for tests in C# and one for jasmine tests in JavaScript files.

 C# TDD Tests

We base the TDD tests template on the naming convention used in Roy Oshroves book: The art of unit testing.

In the live template explorer:

  • Select c# in the Scopes selection
  • Click the “New Template”
  • Paste in the following in the dialog box:

[Test]
public void $Method$_$Scenario$_$ExpectedResult$()
{
// Arrange

// Act
$END$

// Assert
}
  • Type in the shortcut you want to use and possible a description
  • Now in the editor while you are editing a c# file you can simply type “test” and it will be replaced by the code snippets. The variables denoted with $ signs help you remember what goes where. The special $END$ marks the last place for the cursor after you have entered values for all the required variables. You shift between the different variables by pressing enter.

Let us create a template for creating BDD tests in the Jasmine framework as well.

Javascript BDD Tests

Follow the same instructions as the C# test but instead of selecting C# in the Scopes section you obviously select JavaScript. This is my template


it("$Functionality$", function() {
 // Arrange

 // Act
 $END$
 // Assert
});
<pre>

Live Template Explorer

Published in programming tips&tricks

Comments

Leave a Reply

Your email address will not be published.