<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abú-Bakr Pereira Kebé</title>
    <description>The latest articles on DEV Community by Abú-Bakr Pereira Kebé (@bacarpereira).</description>
    <link>https://dev.to/bacarpereira</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F573905%2Fabea4aa6-1591-45c1-96e2-1b452930bb1d.jpeg</url>
      <title>DEV Community: Abú-Bakr Pereira Kebé</title>
      <link>https://dev.to/bacarpereira</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bacarpereira"/>
    <language>en</language>
    <item>
      <title>Don´t use Singleton Pattern in your unit tests</title>
      <dc:creator>Abú-Bakr Pereira Kebé</dc:creator>
      <pubDate>Sat, 12 Jun 2021 14:35:07 +0000</pubDate>
      <link>https://dev.to/bacarpereira/don-t-use-singleton-pattern-in-your-unit-tests-8p7</link>
      <guid>https://dev.to/bacarpereira/don-t-use-singleton-pattern-in-your-unit-tests-8p7</guid>
      <description>&lt;h2&gt;
  
  
  What is Singleton Pattern?
&lt;/h2&gt;

&lt;p&gt;Is a design pattern that restricts the instantiation of a class to a single instance. This is useful when one object is needed to coordinate actions across the system exemple custom spiner and services. Through this design pattern, the singleton class ensures that it´s only instantiate once, and can provide easy access to the single instante.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unit Testing
&lt;/h2&gt;

&lt;p&gt;Is a type of software testing where individual units or componentes of a software are tested. Unit tests isolate a section of code and verify its correctness. A unit test may be an individual function, method, procedure, module or object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do some programmers use singleton in testing?
&lt;/h2&gt;

&lt;p&gt;Some programmers use Singleton in their tests with the intention of using the &lt;strong&gt;DRY (Don´t repeat yourself)&lt;/strong&gt; principle by instantiating the classes that will be used in the tests only once.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, why don't use design pattern singleton in Unit tests?
&lt;/h2&gt;

&lt;p&gt;Considering that one of the best practices in unit testing is that tests should be independent and in case of any change in one test the others should not be affected, the singleton pattern hinders testability.&lt;/p&gt;

&lt;p&gt;Singletons cause implicit dependencies between conceptually independent units of code. This is problematic both because they are hidden and because they introduce unnecessary coupling between units.&lt;/p&gt;

&lt;p&gt;As there is no control over creation, a clean instance of the object cannot be used for each test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;I'm going to use &lt;strong&gt;TypeScript&lt;/strong&gt; and &lt;strong&gt;Jasmine&lt;/strong&gt; testing framework in this example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State.ts Model&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class State {
  id: number;
  status: string;
  static instance:State;

  constructor(args: {id:number,status: string}) {
    this.id = args.id;
    this.status = args.status;
  }

  static getInstance():State{
    if(!State.instance){
      State.instance = new State({id:1995,status:'LOADING'});
    }
    return State.instance;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;state.spec.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { State } from .....;

describe('State', () =&amp;gt; {
  let state:State = State.getInstance();

  it('UT1 - should state id and status be correct', () =&amp;gt; {
    expect(state.id).toEqual(1995); // Will always pass
    expect(state.status).toEqual('LOADING'); // Will always pass
    state.status = 'STOP';
  });

  it('UT2 - should state status be LOADING', () =&amp;gt; {
    expect(state.status).toEqual('LOADING'); // It will fail whenever UT1 is run first because the state will always change to STOP
  });

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;UT1 (Unit Test 1) and UT2 share state of the same instance of a class causing &lt;strong&gt;unnecessary coupling between unit tests&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To prevent this from happening we need to perform some setup.&lt;/p&gt;

&lt;p&gt;These activities are called setup and teardown (for cleaning up) and Jasmine has function we can use to make this easier.&lt;/p&gt;

&lt;p&gt;First we need to add this code in &lt;strong&gt;State.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static getCleanInstance():State{
    return new State({id:1995,status:'LOADING'});
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we add this code in &lt;strong&gt;state.spec.ts&lt;/strong&gt;&lt;br&gt;
In each test we get a new instance of state eliminating the dependency between tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let state:State;
  beforeEach(() =&amp;gt; {
     state = State.getCleanInstance();
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Singleton classes do not allow for Test Driven Development (TDD) &lt;a href="https://dev.to/bacarpereira/the-laws-of-tdd-1p1c"&gt;The laws of TDD&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Unit testing depends on tests being independent of one another, so the tests can be run in any order and the program can be set to a known state before the execution of every unit test. Once you have introduced singletons with mutable state, this may be hard to achieve. In addition, such globally accessible persistent state makes it harder to reason about the code, especially in a multithreaded environment.&lt;/p&gt;

&lt;p&gt;The Singleton pattern solves many of your problems. You know that you only need a single instance. You have a guarantee that this instance is initialized before it’s used. It keeps your design simple by having a global access point, but don’t use in your unit tests.&lt;/p&gt;

&lt;p&gt;Cheers ✌️,&lt;br&gt;
Bc.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>cleancode</category>
      <category>testdev</category>
      <category>tdd</category>
    </item>
    <item>
      <title>The laws of TDD</title>
      <dc:creator>Abú-Bakr Pereira Kebé</dc:creator>
      <pubDate>Sun, 14 Mar 2021 11:30:40 +0000</pubDate>
      <link>https://dev.to/bacarpereira/the-laws-of-tdd-1p1c</link>
      <guid>https://dev.to/bacarpereira/the-laws-of-tdd-1p1c</guid>
      <description>&lt;h2&gt;
  
  
  What is TDD?
&lt;/h2&gt;

&lt;p&gt;TDD - Test Driven Development is a software development approach in which test cases are developed to specify and validate what the code will do. In TDD the tests are not done at the end of the software development, the tests are done before writing any production code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use TDD?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Is your code difficult to test?&lt;/li&gt;
&lt;li&gt;Don't you know if you have 100% test coverage on the project?&lt;/li&gt;
&lt;li&gt;Are you afraid to make changes to the project because you don't know where it will go wrong?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TDD solves these and other problems because with the laws of tdd every single line of code that you write will be tested.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three laws of TDD
&lt;/h2&gt;

&lt;p&gt;1.You are not allowed to write any production code until you have first written a failing unit test;&lt;br&gt;
2.You are not allowed to write more of a unit test than is sufficient to fail and not compiling is failing;&lt;br&gt;
3.You are not allowed to write more production code that is sufficient to pass currently failing unit test.&lt;/p&gt;

&lt;p&gt;You begin by writing a small portion of unit test. But within a few seconds you must mention the name of some class or function you have not written yet, thus causing the unit test to fail to complete. So you must write production code that makes the test compile. But you cant write any more code than that, so you start writing more unit test code.&lt;/p&gt;

&lt;p&gt;Round and round the cycle you go. Adding a bit to the test code. Adding a bit to the production code. The two code streams grow simultaneously into complementary components. The tests fit the production code like an antibody fits an antigen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;TDD allows us to be more productive since the project only grows &lt;strong&gt;“if I can say so”&lt;/strong&gt; after all the tests have passed.&lt;/p&gt;

&lt;p&gt;Once the TDD is embraced it will be a step towards the Clean Code.&lt;/p&gt;

&lt;p&gt;In the TDD scenario developers need to be very disciplined because sometimes when the calendar starts to get tight some tests may start to be ignored and if you haven't tested the code how will you know there is a hidden bug ?.&lt;/p&gt;

&lt;p&gt;Cheers,&lt;br&gt;
&lt;strong&gt;Bc&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>tdd</category>
      <category>cleancode</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
