uncategorized

ReSharper Live Templates

One of the nicest features in ReSharper is Live Templates.  At work during the last week I was writing a lot of mock tests and I was getting tired of tapping out the same thing over and over.  I was already using a Mock Test File Template that I’d written to speed up my use of Rhino Mocks, but it didn’t help any with the repeated creation of mock objects.  I wrote this one liner that requires you to only enter the mock type and the name (which even provides a pre-determined list of names). 

Making it work:

  1. Download the Create Mock Live Template here and the Mock Test File Template here
  2. Open the ReSharper | Options menu (Alt-R, O)
  3. For the Create Mock Live Template, navigate to the Templates, Live Templates node in the menu tree on the left hand side of the Options window
  4. Select the User Templates item in the Available Templates area in the upper right of the Options window
  5. Click the Import Templates from File button and select the file you downloaded in step one
  6. For the Mock Test File Template, navigate to the Templates, File Templates node in the menu tree on the left hand side of the Options window
  7. Select the User Templates item from the Available Templates area in the upper right of the Options window
  8. Click the Import Templates from File button and select the file you downloaded in step one
  9. Click ‘OK’ to save the changes.
  10. For the File Template to work you will need to re-open the ReSharper Options menu and click the Reset Shortcuts button on the General menu node
  11. Back in the IDE you will now be able to type ‘cm’ followed by the Tab key to start the process of creating a new mock object, and use the Alt-R, N, M to create a new mock testing class

This Live Template was written specifically to work with my Mock Test File Template which creates the module level _mockery variable.  If you aren’t planning on using the Mock Test File Template then you will need to tweak the Create Mock Live Template.