BDD macro and Reshaper Template

My colleague, Joe Ocampo, created a ReSharper template:

   1: [Test]
   2: public void 
   3:    "$Specification$"()

To add the template, go to ReSharper->Options in the menu:

resharper_menu

Then choose “Live Templates” underneath the Templates heading from the items on the left of the popup window.  Under the “User Templates” section create a new template called “test” with the text above:

resharper_live_templates

When you write your unit test fixture with the “Given, When, Should” format, these items come in handy.  Once you get ready to write your test, you can just type the word “test”, then press tab and get the following in Visual Studio:

image

Ignore the red underlines and why the “TestName” word is on the next line.  The red lines will disappear after all of this is complete and the new line is necessary for the Visual Studio macro we’re about to create (so it can parse your test method name correctly).

You have to add a macro to Visual Studio via the Tools->Macros->Macros Explorer menu option (or use Alt+F8):

 

macros_menu

This will open the macros window.  Right-click “MyMacros” and choose “New Module”:

macros_window

Name the module TestMethodNameFormatterForBDD (or whatever you want to call it):

new_macro_window

This will add it back to the Macros window.  Double-click the new macro or right-click and choose “Edit”.  This will open the Microsoft Visual Studio Macros (MVSM) IDE.  Select all and replace the text with the following:

   1: Imports System
   2: Imports EnvDTE
   3: Imports EnvDTE80
   4: Imports System.Diagnostics
   5:  
   6: Public Module TestMethodNameFormatterForBDD
   7:     Sub ReplaceSpacesWithUnderscores()
   8:         Dim selectedText As String
   9:  
  10:         DTE.ActiveDocument.Selection.EndOfLine()
  11:         DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, True)
  12:         selectedText = DTE.ActiveDocument.Selection.Text
  13:         DTE.ActiveDocument.Selection.Text = selectedText.Replace(" ", "_").Replace(Chr(34).ToString(), "")
  14:         DTE.ActiveDocument.Selection.LineUp()
  15:         DTE.ActiveDocument.Selection.EndOfLine()
  16:         DTE.ExecuteCommand("Edit.Delete")
  17:         DTE.ActiveDocument.Selection.EndOfLine()
  18:         DTE.ActiveDocument.Selection.NewLine()
  19:         DTE.ActiveDocument.Selection.Text = "{"
  20:         DTE.ExecuteCommand("Edit.BreakLine")
  21:     End Sub
  22: End Module

 

Should look like this:

MVSM_Window

Choose File->Save and then close the MVSM window.  Back at Visual Studio choose Tools->Options and go to the keyboard option on the left of the popup.  In the “Show commands containing:” TextBox, type “BDD” and your new Macro should show in the list.  I personally like the Ctrl+` (tick – to the left of the number 1 on the keyboard) for my keyboard assignment, but choose whatever you like.  Just type that choice into the “Press shortcut keys:” TextBox and click the Assign button.

keyboard_setting

Now when you type your “test” keyword that triggers the ReSharper template, just type out the test name with spaces:

   1: [Test]
   2: public void 
   3:  "Should render the index view when the index action is selected"()

Your cursor will be at the end of the word “selected”.  Press your keyboard command, mine being Ctrl+`.  The macro will kick-in and you will see your spaces replaced with underscores and your method name moved up to the same line as the public void declaration.

   1: [Test]
   2: public void Should_render_the_index_view_when_the_index_action_is_selected()
   3: {
   4:     homeController.Index();
   5:     Assert.AreEqual(@"Homeindex", homeController.SelectedViewName);
   6: }
Hope this helps someone else to automate replacing spaces with underscores, therefore allowing you to type your test method names out like a sentence then utilize tools around you to make it compliant with the C# syntax.

Enjoy!

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jason Meridth

Continuously learning software developer trying to not let best be the enemy of better
This entry was posted in Programming, testing, Visual Studio. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Comments are closed.