<?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: Ainama Sylvain💙</title>
    <description>The latest articles on DEV Community by Ainama Sylvain💙 (@ainamasylvain).</description>
    <link>https://dev.to/ainamasylvain</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%2F167381%2F0171a7ee-d5a1-4887-9c05-26f25f83d5ff.jpg</url>
      <title>DEV Community: Ainama Sylvain💙</title>
      <link>https://dev.to/ainamasylvain</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ainamasylvain"/>
    <language>en</language>
    <item>
      <title>PHPUNIT:Demystifying the double test (Dummy,Stub,Mock)</title>
      <dc:creator>Ainama Sylvain💙</dc:creator>
      <pubDate>Tue, 08 Sep 2020 19:04:01 +0000</pubDate>
      <link>https://dev.to/ainamasylvain/demystifying-the-double-test-dummy-stub-mock-4ae</link>
      <guid>https://dev.to/ainamasylvain/demystifying-the-double-test-dummy-stub-mock-4ae</guid>
      <description>&lt;h1&gt;
  
  
  Description
&lt;/h1&gt;

&lt;p&gt;This article will help you differentiate between a Dummy, a Stub and a Mock.&lt;/p&gt;

&lt;h1&gt;
  
  
  Test Double
&lt;/h1&gt;

&lt;p&gt;In unit testing often, we often get to test classes.&lt;br&gt;
Classes without dependencies are easy to test, but with dependencies it becomes much more complicated.&lt;br&gt;
We can test a class with all its dependencies but it can be very complicated. The best solution is to isolate it from all its dependencies.&lt;br&gt;
This is when the liners come in.&lt;br&gt;
Take for example an actor often during filming the actor needs an understudy to perform stunts that one asks of him, it is exactly the same principle for the doublings in programming.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fklw90rilkrpy0pe26d6i.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fklw90rilkrpy0pe26d6i.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
The problem with liners is that you usually don't know how to identify them.&lt;br&gt;
Do we use a &lt;strong&gt;mock&lt;/strong&gt;, a &lt;strong&gt;stub&lt;/strong&gt; or a &lt;strong&gt;dummy&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  But what is the difference?
&lt;/h1&gt;

&lt;p&gt;To explain the difference between a &lt;strong&gt;mock&lt;/strong&gt;, a &lt;strong&gt;stub&lt;/strong&gt; or a &lt;strong&gt;dummy&lt;/strong&gt;, we will first see their definitions.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a &lt;strong&gt;Dummy&lt;/strong&gt;?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;dummy&lt;/strong&gt; is an empty class that we can use when we need to instantiate an object dependent on it.&lt;br&gt;
It aims to make believe that a class exists to the one that we want to test.&lt;br&gt;
It is usually passed as a parameter but never called.&lt;br&gt;
If called, its functions will always return null.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

  &lt;span class="nv"&gt;$client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'GuzzleHttp\Client'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  What is a &lt;strong&gt;Stub&lt;/strong&gt;?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Stub&lt;/strong&gt; is a &lt;strong&gt;dummy&lt;/strong&gt; that will be customizable.&lt;br&gt;
It will correspond to what we expect.&lt;br&gt;
He can redefine his methods and tell him what to return.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

  &lt;span class="nv"&gt;$stub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;createMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Stubclass&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
              &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'someStubMethod'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
              &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;willReturn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'foo'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nv"&gt;$someClass&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;someStubMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$stub&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  What is a &lt;strong&gt;Mock&lt;/strong&gt;?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;mock&lt;/strong&gt; is a duplicate that checks expectations.&lt;br&gt;
We can make it check that the method is called &lt;strong&gt;x&lt;/strong&gt; times like &lt;strong&gt;never&lt;/strong&gt;.&lt;br&gt;
If the simulation is not done as many times as expected the test will fail.&lt;br&gt;
Unlike the &lt;strong&gt;Stub&lt;/strong&gt; a &lt;strong&gt;Mock&lt;/strong&gt; can &lt;strong&gt;throw exceptions&lt;/strong&gt; if it does not receive the correct calls.&lt;br&gt;
The purpose of the mock is not to be interested in the return value but rather to be interested in the method which is called and to know &lt;strong&gt;how many times&lt;/strong&gt; and with what &lt;strong&gt;arguments&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The execution will be launched once&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

  &lt;span class="nv"&gt;$mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;createMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Mockclass&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="c1"&gt;// Once() one time&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;expects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;once&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'someMockMethod'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;willReturn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'foo'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nv"&gt;$someClass&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$mock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;The execution will never be launched&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="nv"&gt;$mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;createMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Mockclass&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;expects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;never&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;br&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'someMockMethod'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;willReturn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'foo'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="nv"&gt;$someClass&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$mock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h1&gt;

&lt;p&gt;Now you will no longer confuse &lt;strong&gt;Mock&lt;/strong&gt;, &lt;strong&gt;Stub&lt;/strong&gt; and &lt;strong&gt;Dummy&lt;/strong&gt;.&lt;br&gt;
&lt;strong&gt;Now you will see more clearly !!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F78dm2mdbp2ww6wuhzzk5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F78dm2mdbp2ww6wuhzzk5.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>beginners</category>
      <category>testing</category>
      <category>phpunit</category>
    </item>
  </channel>
</rss>
