{"id":200,"date":"2009-10-23T09:20:46","date_gmt":"2009-10-23T07:20:46","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=200"},"modified":"2009-10-23T09:20:46","modified_gmt":"2009-10-23T07:20:46","slug":"unit-testing-is-good","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/unit-testing-is-good","title":{"rendered":"Unit testing is good"},"content":{"rendered":"<p>Recently Ayende added another <a href=\"http:\/\/ayende.com\/Blog\/archive\/2009\/10\/21\/challenge-can-you-spot-the-bug.aspx\">spot-the-bug post<\/a>. Usually I&#8217;m just curious how fast people find the problem.<\/p>\n<p>Some time ago he also published few posts about how he does unit testing.<\/p>\n<p>In <a href=\"http:\/\/ayende.com\/Blog\/archive\/2009\/09\/29\/scenario-driven-tests.aspx\">&#8220;Scenario driven tests&#8221;<\/a> he says that, as I understand it, it&#8217;s better to <strong>test a whole scenario<\/strong> not a single method. I&#8217;m okay with that, as long as scenarios are in <strong>reasonable numbers<\/strong> and <strong>code coverage is high<\/strong>.<\/p>\n<p>In <a href=\"http:\/\/ayende.com\/Blog\/archive\/2009\/09\/28\/even-tests-has-got-to-justify-themselves.aspx\">&#8220;Tests have got to justify themselves&#8221;<\/a> he states <strong>&#8220;I am not using TDD.&#8221;<\/strong><\/p>\n<p>Well if you were you would not have so many problems in 10 lines of code, period.<br \/>\nYou want to test scenarios ok, but when you&#8217;ve simple method with lots of logic, <strong>you are going to make a mistake at some point<\/strong>. Test it!<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic static string FirstCharacters(this string self, int numOfChars)\n{\n    if (self == null)\n        return &quot;&quot;;\n    if (self.Length &lt; numOfChars)\n        return self;\n    return self\n        .Replace(Environment.NewLine, &quot; &quot;)\n        .Substring(0, numOfChars - 3) + &quot;...&quot;;\n}\n<\/pre>\n<p>Each of the following tests will fail:<br \/>\n(negative values are not reasonable input for such method, so we won&#8217;t go there)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;Test]\npublic void FirstCharacters_EmptyString_Truncates()\n{\n    Assert.AreEqual(&quot;&quot;, &quot;&quot;.FirstCharacters(0));\n}\n\n&#x5B;Test]\npublic void FirstCharacters_RegularString_Truncates()\n{\n    Assert.AreEqual(&quot;12345&quot;, &quot;12345&quot;.FirstCharacters(5));\n}\n\n&#x5B;Test]\npublic void Method_Condition_Result()\n{\n    Assert.AreEqual(&quot;...&quot;, &quot;12345&quot;.FirstCharacters(2));\n}\n\n&#x5B;Test]\npublic void FirstCharacters_StringWithNewLine_Truncates()\n{\n    Assert.AreEqual(&quot;  &quot;, &quot;rnrnrn&quot;.FirstCharacters(2));\n}\n\n&#x5B;Test]\npublic void FirstCharacters_StringWithNewLine_Truncates2()\n{\n    Assert.AreEqual(&quot; ...&quot;, &quot;nnnnnnnn&quot;.FirstCharacters(4));\n}\n\n&#x5B;Test]\npublic void FirstCharacters_StringWithNewLine_Truncates3()\n{\n    Assert.AreEqual(&quot;start end&quot;, &quot;startnend&quot;.FirstCharacters(255));\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently Ayende added another spot-the-bug post. Usually I&#8217;m just curious how fast people find the problem. Some time ago he also published few posts about how he does unit testing. In &#8220;Scenario driven tests&#8221; he says that, as I understand it, it&#8217;s better to test a whole scenario not a single method. I&#8217;m okay with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[15,56],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-programming","tag-c","tag-unit-testing"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/200"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":0,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}