{"id":2977,"date":"2023-07-24T20:29:56","date_gmt":"2023-07-24T18:29:56","guid":{"rendered":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2977"},"modified":"2023-07-26T18:01:01","modified_gmt":"2023-07-26T16:01:01","slug":"tests","status":"publish","type":"post","link":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2977","title":{"rendered":"TDD"},"content":{"rendered":"<p style=\"text-align: justify;\">Since our school years we have been conducted to tests. Class tests, which provided if pupil knows given subject, medical tests proving status about our health, or even surveys providing knowledge for research purposes. Named things were destined for one thing &#8211; to give information about something and in the result, to draw exact conclusions.&nbsp;<\/p>\n<p style=\"text-align: justify;\">Programming provides tests as well. Today there are many principles of writing <a style=\"font-weight: bold;\" href=\"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?cat=13\">clean code&nbsp;<\/a>and methods of testing, which should lead programmers. Test Driven Development is an approach of software creating and testing &#8211; this approach assumes, that before writing appropriate functionality, developer should write a test, which very often at the beginning fails everything &#8211; after failure, developer writes correctly code with proper functionalities.&nbsp;<\/p>\n<p><style>\/*! elementor - v3.14.0 - 26-06-2023 *\/<br \/>\n.elementor-widget-image{text-align:center}.elementor-widget-image a{display:inline-block}.elementor-widget-image a img[src$=\".svg\"]{width:48px}.elementor-widget-image img{vertical-align:middle;display:inline-block}<\/style>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"\" src=\"https:\/\/res.cloudinary.com\/kentcdodds-com\/image\/upload\/f_auto,q_auto,dpr_2.0,w_1600\/v1625033508\/kentcdodds.com\/content\/blog\/when-i-follow-tdd\/0.jpg\" alt=\"\" width=\"690\" height=\"706\"><\/p>\n<p style=\"text-align: center;\">Picture above displays three steps in TDD technique.&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"color: #ff0000;\">Failing Step<\/span>&nbsp;&#8211;&nbsp;<\/strong>Firstly, test is written &#8211; it cannot be passed, because of functionality which can&#8217;t be implemented. It is possible, that even after writing such a test, the code won&#8217;t work. IDE displays test as red coloured.&nbsp;&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"color: #99cc00;\"><strong>Second<\/strong>&nbsp;<\/span><strong><span style=\"color: #99cc00;\">Step<\/span>&nbsp;<\/strong>&#8211; relies on writing test which implements missing in first step functionality. This case establishes writing code which is not perfectly clean &#8211; it is about quickly writing missing functionality. After that, we confirm that our implementation is good enough by launching tests. If everything is correct, IDE should light on green colour.&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"color: #0000ff;\"><strong>Refactor<\/strong><\/span>&nbsp;&#8211; last step of TDD technique. This step has to clean up redundant mess created before, but not changing written functionality. Giving an example &#8211; creating a method which deletes duplicated code, or even creating another class responsible for partially other class.&nbsp;<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n public class UnitTest\n    {\n        &#91;Fact]\n        public void addingToValuesToDatabase()\n        {\n            \/\/arrange\n            Database database= new Database();\n        }\n    }\n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">Created code above doesn&#8217;t pass test because of of missing Database class &#8211; first step of test failing is check.&nbsp;<\/p>\n<p>Let&#8217;s create Database class:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Database { }\n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">This time, code passes test correctly &#8211; we&#8217;ve made 2nd step. 3rd step is to refactor our code, however we don&#8217;t have to refactor anything in our code, then one cycle of test development is made correctly. Now we add new lines of code:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class UnitTest\n{  \n    &#91;Fact] \n    void addingToValuesToDatabase()  \n    {  \n        \/\/arrange\n        Database database = new Database();  \n        Value value= new Value(1, &quot;Contact&quot;);  \n\n        \/\/act\n        database.add(value);  \n    }  \n}\n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">This time, code doesn&#8217;t pass the test, due to missing&nbsp;<em>add<\/em> method in Database class. Now it&#8217;s time to create&nbsp;<em>add<\/em> method to class:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Database{\n\n       public void add(Value value){ }\n\n}\n<\/pre><\/div>\n\n<p>In this moment, code should pass the test. Refactor is not yet required &#8211; 2nd cycle of tests has been made. What is worth to pay attention, is every step is iterating like in standard loop &#8211; we add only some parts of code, which are crucial to pass the test, no more. Next steps are made similar &#8211; adding some code, checking if test fails, then creating the code to pass the test, and at the end its only a matter of refactor, to keep code clean.<\/p>\n<h5 style=\"text-align: center;\">Three musketeers of Unit Tests<\/h5>\n<h5>Stub&nbsp;<\/h5>\n<p style=\"text-align: justify;\">Stubs are examples of code implementation, which behaviour we are willing to test &#8211; here we give an example implementation of interface, in which we pass some example data. They are perfect for simple methods, unfortunately not for complex interfaces.<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#91;Fact]\npublic void ValidateAgeExample()\n{\n    var ageValidate= new AgeValidator();\n\n    var ageMock= new Mock&lt;IAge&gt;();\n    ageMock.Setup(c =&gt; c.GetAge()).Returns(21);\n    var age= ageMock.Object;\n\n    bool validate = ageValidator.Validate(age);\n    validate.Should().BeTrue();\n}\n<\/pre><\/div>\n\n<h5>Mock<\/h5>\n<p style=\"text-align: justify;\">Mock is able to verify behaviour of tested object, so it&#8217;s purpose is to check if given component is performed. Mocks are object, which simulate real behaviours of real objects and code. They are made during program&#8217;s work, and are more flexible than stub&#8217;s.&nbsp;<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#91;Fact]\npublic void ValidateAgeExample()\n{\n    var ageValidator= new AgeValidator();\n\n    \n    var ageMock= new Mock&lt;IAge&gt;();\n    ageMock.Setup(c =&gt; c.GetAge()).Returns(21);\n    var age= ageMock.Object;\n\n    \n    ageValidator.Validate(age);\n\n    ageMock.Verify(x =&gt; x.GetAge(), Times.Once);\n}\n<\/pre><\/div>\n\n<h5>Spy<\/h5>\n<p style=\"text-align: justify;\">In simple terms &#8211; its a mock with additional functionality, but spy checks quantity of calls. Its an wrapping object, which wraps an object of given class and in the end it tracks and verifies mocking objects and methods. Object spy is partially a mock, and partially a normal object<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#91;Fact] \npublic void ValidateAgeExample()\n    {\n        \n        var ageValidator= new AgeValidator();\n\n        var ageSpy= new Mock&lt;IAge&gt;();\n        var age= ageSpy.Object;\n\n        ageValidator.Validate(age);\n\n        ageSpy.Verify(x =&gt; x.GetAge(), Times.Once);\n    }\n<\/pre><\/div>\n\n<p>To sum up, TDD is very crucial programming&#8217;s element &#8211; it allows developers in easier and faster way find bugs in code, tests are flexible and the results are instant.&nbsp; but it has disadvantages as well &#8211; longer time of development, or time consuming for development preparation.<\/p>","protected":false},"excerpt":{"rendered":"<p>Basic knowledge about code testing.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[1,3,15],"tags":[],"class_list":["post-2977","post","type-post","status-publish","format-standard","hentry","category-bez-kategorii","category-programming-articles","category-test"],"_links":{"self":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2977"}],"version-history":[{"count":27,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2977\/revisions"}],"predecessor-version":[{"id":3023,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2977\/revisions\/3023"}],"wp:attachment":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}