ReSharper Live Templates for Unit Test Classes

2 Flares Twitter 1 Facebook 0 StumbleUpon 0 Google+ 1 LinkedIn 0 2 Flares ×

I tend to adopt a very similar pattern when writing most of my test classes – mostly because it enables me to write quick helper classes and ReSharper templates for repetitive tasks. ReSharper live templates are a great way to speed up the writing of unit tests – you can use visual studio snippets, but they’re so much more sumbersome to write. Here are a couple I use regularly:

Test Class

I like to configure testc to output a basic test class – like so:

Normally $TestTarget$ refers to the name of the class I’m testing. I always call the object that I’m testing target as it helps me write other templates to speed things up.


I also configure test to output a basic test method:

As I always call the field for the target of my test ‘_target’, the calling of the method is right there for me. This doesn’t work for all scenarios, but it does get me started pretty quick. I also usually use a behavioural naming convention for my method name – i.e. method I’m testing – under foo condition – produces bar results – for example “Save_HasInvalidData_ThrowsValidationException”.


This is a really simple one – I normally use Moq for mocks and stubs – this is a really short template that I use in a specific scenario (which I’ll explain afterwards):

Its obvious (to users of Moq) that this is just creating a mock of a specified interface. I tend to use this when building up the constructor for my target object.

Usually you’ll want actual fields for the mock, but its so quick to create the mock like this inline, highlight the portion up until the .Object and use more ReSharper magic to extract the field.

, ,

The Essential App Marketing Kit
Subscribe To My Newsletter To Get an Entire Chapter From The Book for FREE
Never display this again
2 Flares Twitter 1 Facebook 0 StumbleUpon 0 Google+ 1 LinkedIn 0 2 Flares ×