Code Snippet for Xamarin Month
This Xamarin Month was especially interesting to me because I love code snippets! They save lots of time by stubbing in repetitive code with just a few keystrokes. The snippet I’m posting is one I use almost every day and helps provide a consistent name and outline when writing unit tests.
It’s All in the Name
We write our unit tests using NUnit and follow the Microsoft naming convention for all tests so anyone on the team can quickly understand the intent of the test and get all the necessary info just by reading its name. That convention breaks the name into three parts:
- The name of the method or unit under test
- The scenario or state under which it’s being tested
- The expected behavior or result when the scenario is invoked
These parts are sometimes hard for me to remember, so this snippet gives me some clues to jog my memory.
We also structure every unit test we write using the same basic outline, so anyone on the team can more easily get up to speed on an unfamiliar test and understand what it’s doing. That outline is known as the Triple-A (AAA) Pattern: Arrange, Act, Assert. You can read more about that here.
I set up this snippet to use the shortcut
test, so by typing that and pressing tab twice, voila! The whole unit test is stubbed out for me:
The snippet gives me a clue on what the first part of the name should be,
UnitUnderTest. This snippet uses variables, so I can enter the first part of the name and press tab to move on to the other 2 parts of the name:
I’ll provide the snippet in two formats.
The first one you can copy/paste into Visual Studio for Mac by going to Preferences > Text Editor > Code Snippets > C# group, and creating a new code snippet.
The 2nd format is the entire snippet file as VS for Mac saves it. Just drop the file here:
~/Library/VisualStudio/7.0/Snippetsand you’re good to go. (You may have to restart VS)
That’s it! Hope this saves you a little time and typing! Please reach out to me on twitter if you have any comments or feedback.