{"id":3761,"date":"2015-05-20T13:00:12","date_gmt":"2015-05-20T13:00:12","guid":{"rendered":"http:\/\/writeasync.net\/?p=3761"},"modified":"2015-05-20T04:09:16","modified_gmt":"2015-05-20T04:09:16","slug":"destroy-all-tests-unit-test-edition","status":"publish","type":"post","link":"http:\/\/writeasync.net\/?p=3761","title":{"rendered":"Destroy all* tests: unit test edition"},"content":{"rendered":"<p>Sometimes the code we need to write is so simple that we cannot imagine getting it wrong. Going <a href=\"http:\/\/www.extremeprogramming.org\/rules\/testfirst.html\">test-first<\/a> in these cases just seems apt to lead to tedium and <a href=\"http:\/\/stackoverflow.com\/questions\/20256851\/how-to-avoid-writing-a-tautological-test\">tautologies<\/a>. This could be a fair point if the sole purpose of unit testing were bug-finding. But there is so much more to it than that as <a href=\"http:\/\/jbazuzicode.blogspot.com\">Jay Bazuzi<\/a> points out in his <a href=\"http:\/\/jbazuzicode.blogspot.com\/search\/label\/Why%20we%20Test\">&#8220;Why We Test&#8221;<\/a> series. A test can be equally good as a safety net and an <a href=\"http:\/\/www.c2.com\/cgi\/wiki?TheUnitTestIsTheSpecification\">executable spec<\/a>.<\/p>\n<p>So how do we skip the drudgery but keep the value? One powerful technique is described by the <a href=\"http:\/\/xunitpatterns.com\/Testcase%20Superclass.html\">Testcase Superclass pattern<\/a>. Basically you write the boring stuff once, in essence &#8220;generalizing the specification.&#8221; Then you write just enough test code to wire up your domain object to this canned test suite.<\/p>\n<p>On the web you can find a few examples of this approach applied to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Value_object\">value objects<\/a>: Kenneth Xu&#8217;s <a href=\"http:\/\/kennethxu.blogspot.com\/2009\/11\/unit-test-value-object.html\">&#8220;Unit Test Value Object \u2013 Reusable Test Fixture&#8221;<\/a> and Jared Parsons&#8217; <a href=\"http:\/\/blogs.msdn.com\/b\/jaredpar\/archive\/2010\/04\/30\/properly-testing-equality.aspx\">&#8220;Making Equality Testing Simple&#8221;<\/a>.<\/p>\n<p>With a bit of effort and some extension methods you can even shoot for <a href=\"http:\/\/www.fluentassertions.com\/\">Fluent Assertions<\/a>-style readability:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;TestMethod]\r\npublic void ShouldHaveValueEqualitySemantics()\r\n{\r\n    ValueObject.OfType&lt;MyObj&gt;().WithExamples(() =&gt; new MyObj(1), () =&gt; new MyObj(2)).ShouldHaveValueEqualitySemantics();\r\n}\r\n<\/pre>\n<p>(Implementation left as an exercise to the reader.) \ud83d\ude00<\/p>\n<p>Now go ahead and destroy those bland and repetitive unit tests without fear of future fallout.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes the code we need to write is so simple that we cannot imagine getting it wrong. Going test-first in these cases just seems apt to lead to tedium and tautologies. This could be a fair point if the sole&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,51],"tags":[],"class_list":["post-3761","post","type-post","status-publish","format-standard","hentry","category-tdd","category-testing"],"_links":{"self":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/3761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3761"}],"version-history":[{"count":0,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/3761\/revisions"}],"wp:attachment":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3761"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}