One of the things I really enjoy about the local developer groups is the sharing of ideas and usage patterns of common tools. At tonight's Ann Arbor .net Developers Group, the topic of CodeRush templates came up. I commonly use a set of templates centered around the Rhino Mocks framework As I was describing these templates Jay Wren claimed they should be posted. The irony in that is the first time I saw someone using CodeRush was in a presentation Jay was giving on IoC. As a result of that presentation, we are using Castle Windsor in our application and CodeRush as a productivity tool.
Creating a Mock Instance
To use Rhino Mock to create a mock instance, you write something like:
MyType myType = (MyType)mocks.DynamicMock(typeof(MyType));
This is an exceptionally redundant exercise that just screams for a Template. The core part of the template takes a string from the Get string provider (named Type) for the type of the instance to create. It will also name the variable with the same name as the type with a slightly different format. Even though this variable name is a linked field, you can break the link by pressing Ctrl-Enter while the cursor is on the variable name. This is commonly required in when creating multiple mock instances in the same scope.
#MockInstance#: Base template not intended to be called directly
mk: the type name is initialized to MyType
mk\: the type name is initialized from the Clipboard
Setting up the Tests
The mock instance templates are not much use without having the MockRepository set up and test methods to call. These templates come in handy as well.
mtf: the test fixture
public class «Caret»My«BlockAnchor»Test
private MockRepository mocks;
public void Setup()
mocks = new MockRepository();
mt: the test
public void «Caret»Test«BlockAnchor»()
Hopefully these templates will be found useful and maybe even kick off some ideas for new ones.
UPDATE: Yea, it would be easier if I added the export file.CSharp_Custom.xml (12.45 KB)