{"id":126,"date":"2019-07-16T15:01:43","date_gmt":"2019-07-16T14:01:43","guid":{"rendered":"https:\/\/wissing.me\/?p=126"},"modified":"2019-07-16T15:01:43","modified_gmt":"2019-07-16T14:01:43","slug":"unit-test-snippets","status":"publish","type":"post","link":"https:\/\/daan.wsng.eu\/index.php\/2019\/07\/16\/unit-test-snippets\/","title":{"rendered":"Unit test snippets"},"content":{"rendered":"\n<p>I am a big fan of making things easy for myself. When preparing the <a href=\"https:\/\/wissing.me\/index.php\/2019\/03\/21\/learning-by-teaching-tdd\/\">TDD Workshop<\/a> for another round I found it increasingly tedious to type out every unit test function. Luckily, Visual Studio lets you create your own snippets, so I created one that would immediately generate something like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#91;Test]\npublic void GivenContext_WhenAction_ThenResult()\n{\n    \/\/ Arrange\n\n    \/\/ Act\n\n    \/\/ Assert\n}\n<\/pre><\/div>\n\n\n<!--more-->\n\n\n\n<p>How does it work I hear you say? It&#8217;s simpler than I expected:<\/p>\n\n\n\n<ol><li>Create a .snippet file in <code>&lt;documents>\/Visual Studio 2017\/Code Snippets\/Visual C#\/My Code Snippets<\/code> (e.g. TDD.snippet)<\/li><li>Put the following XML content in the file:<\/li><\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; highlight: [11,12,13,14,15,16,17,18,19,20,21]; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;CodeSnippets xmlns=&quot;http:\/\/schemas.microsoft.com\/VisualStudio\/2005\/CodeSnippet&quot;&gt;\n  &lt;CodeSnippet Format=&quot;1.0.0&quot;&gt;\n    &lt;Header&gt;\n      &lt;Title&gt;Unit test template&lt;\/Title&gt;\n      &lt;Author&gt;Daan Wissing&lt;\/Author&gt;\n      &lt;Description&gt;Insert Arrange\/Act\/Assert groups in unit test function&lt;\/Description&gt;\n      &lt;Shortcut&gt;unittest&lt;\/Shortcut&gt;\n    &lt;\/Header&gt;\n    &lt;Snippet&gt;\n      &lt;Code Language=&quot;CSharp&quot;&gt;\n        &lt;!&#91;CDATA&#91;&#91;Test]\n        public void Given$context$_When$action$_Then$result$()\n        {\n          \/\/ Arrange\n        \n          \/\/ Act\n        \n          \/\/ Assert\n        }]]&gt;\n      &lt;\/Code&gt;\n      &lt;Declarations&gt;\n        &lt;Literal&gt;\n          &lt;ID&gt;context&lt;\/ID&gt;\n          &lt;Default&gt;Context&lt;\/Default&gt;\n          &lt;ToolTip&gt;The context of this unit test&lt;\/ToolTip&gt;\n        &lt;\/Literal&gt;\n        &lt;Literal&gt;\n          &lt;ID&gt;action&lt;\/ID&gt;\n          &lt;Default&gt;Action&lt;\/Default&gt;\n          &lt;ToolTip&gt;The action that is tested&lt;\/ToolTip&gt;\n        &lt;\/Literal&gt;\n        &lt;Literal&gt;\n          &lt;ID&gt;result&lt;\/ID&gt;\n          &lt;Default&gt;Result&lt;\/Default&gt;\n          &lt;ToolTip&gt;The resulting state that will be asserted&lt;\/ToolTip&gt;\n        &lt;\/Literal&gt;\n      &lt;\/Declarations&gt;\n    &lt;\/Snippet&gt;\n  &lt;\/CodeSnippet&gt;\n&lt;\/CodeSnippets&gt;\n<\/pre><\/div>\n\n\n<p>Done! Visual Studio will automatically pick up the new snippet file (maybe you need to restart). You can use the snippet by invoking IntelliSense (usually CTRL + SPACE), typing <code>unittest<\/code> and hitting TAB. <\/p>\n\n\n\n<p>The highlighted lines are the meat of this snippet: the <code>Code<\/code> node contains what to actually insert in the editor. Most of the rest is some administration, but some extra fun is the <code>Declarations<\/code> node. In this snippet I have to fill in three important pieces of information of the unit test (the context, the action tested and the expectation) because these will change for every unit test.<\/p>\n\n\n\n<p>Try it out yourself!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am a big fan of making things easy for myself. When preparing the TDD Workshop for another round I found it increasingly tedious to type out every unit test function. Luckily, Visual Studio lets you create your own snippets, so I created one that would immediately generate something like this:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"twitterCardType":"","cardImageID":0,"cardImage":"","cardTitle":"","cardDesc":"","cardImageAlt":"","cardPlayer":"","cardPlayerWidth":0,"cardPlayerHeight":0,"cardPlayerStream":"","cardPlayerCodec":""},"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/posts\/126"}],"collection":[{"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":6,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions\/132"}],"wp:attachment":[{"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daan.wsng.eu\/index.php\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}