1. Open Visual Studio.
2. Go to Tools -> Code Snippets Manager.
3. Change the Language to Visual C# and expand the Test folder.
4. Copy the path located in the Location.
5. Open Windows Explorer and browse to the location copied in the previous step.
6. Make a Copy of the testmethod.snippet.
7. If you want to still use the original snippet in Visual Studio, rename it by adding a prefix. If you do not want to use the original snippet, you need to make sure it does not end in .snippet and append something to it or change it. I prefer to keep it so others that are not aware of my new snippet can still use the old one, so I added a prefix of "Orig_".
8. Open testmethod.snippet in notepad, Visual Studio or any other program that will allow you to edit text.
9. Change the text within the Shortcut element from testm to testmm. The reason for the second 'm' is that there are other methods that may show up from IntelliSense and adding the second m will eliminate them from showing up and the snippet will be invoked by typing testmm followed by a tab.
old: <Shortcut>testm</Shortcut>
new: <Shortcut>testmm</Shortcut>
10. Change the text within the Default element from MyTestMethod to to UnitOfWork_StateUnderTest_ExpectedBehavior.
old: <Default>MyTestMethod</Default>
new: <Default>UnitOfWork_StateUnderTest_ExpectedBehavior</Default>
11. Add the following three lines of comments to the method just before the text $end$ within public void $name$()
// Arrange
var x = false;
var y = new y();
// Act
x = y.DoStuff();
// Assert
Assert.IsTrue(x);
12. Save the file.
13. Close all instances Visual Studio if there are any open.
14. Open a Unit Test Project within Visual Studio and set your cursor within a test class.
15. Type testmm followed by a [tab]. As you can see, the snippet will now display the following which has our new method name and the comments to help us create good tests.
Hopefully you learned something from this post and it will help you modify or add more snippets to help you code faster and better.
peace yo!
Perfect. I need to create more of these and also some Class templates. This was just what I was looking for, tired of manually creating method stubs and I do not like all the ones built into VS necessarily. Good stuff here, especially for teams doing Unit Testing (TDD or not, this convention is good).
ReplyDelete