Live Templates . . .

Cheese Pairing for this Post: Beecher's One-Year Aged Flagship Cheddar
[mmm. that's tasty]

Live templates in ReSharper are an excellent time saving tool. They allow you to put code that you use over and over into a shortcut that you don't have to retype it all the time or maintain it in files external to Visual Studio.

I have a few that I've created to make my life easier.

test
[Test]
public void $TESTNAME$()
{
$END$
}

rhino
[Test]
public void $TESTNAME$()
{
$END$
using(mocks.Record())
{

}
using(mocks.Playback())
{

}
}

tostring

public override string ToString()
{
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xmlSerializer = new XmlSerializer(GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF7)
{
Formatting = Formatting.Indented,
Indentation = 1,
IndentChar = '\t'
};
xmlSerializer.Serialize(xmlTextWriter, this);
memoryStream = (MemoryStream) xmlTextWriter.BaseStream;
UTF7Encoding encoding = new UTF7Encoding();
return encoding.GetString(memoryStream.ToArray()).Replace("><", ">\r\n<");
}


This last one works great for figuring out where the differences are when you get an expectation error in one of your tests. Just throw that XML string from the test output into WinMerge and done. Also, I would like to point out for anyone who isn't aware, there is a hotkey in R# to automatically implement IEquatable and override Equals: Alt+Ins, E. Brilliant stuff. The only issue is that you need to update it (Alt+Ins, E will do that too) every time you change your object. That helps with those stubborn objects that tend to get compared by reference when you want to just compare their values.

Sorry about the delay on this post. Apparently, my blog was flagged as spam for a while. All is better not though.

Comments

Popular posts from this blog

Simpler Tests: What kind of test are you writing?

Architecture at different levels of abstraction

TFS to SVN Conversion with History