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:
- Download the Create Mock Live Template here and the Mock Test File Template here
- Open the ReSharper | Options menu (Alt-R, O)
- 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
- Select the User Templates item in the Available Templates area in the upper right of the Options window
- Click the Import Templates from File button and select the file you downloaded in step one
- 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
- Select the User Templates item from the Available Templates area in the upper right of the Options window
- Click the Import Templates from File button and select the file you downloaded in step one
- Click ‘OK’ to save the changes.
- 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
- 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.