<?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>Forem: Landon Marder</title>
    <description>The latest articles on Forem by Landon Marder (@landonmarder).</description>
    <link>https://forem.com/landonmarder</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%2F341217%2Ff86ee11c-0c11-48f6-bdfd-a312899c9062.jpeg</url>
      <title>Forem: Landon Marder</title>
      <link>https://forem.com/landonmarder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/landonmarder"/>
    <language>en</language>
    <item>
      <title>Elixir's Pattern Matching and ExUnit</title>
      <dc:creator>Landon Marder</dc:creator>
      <pubDate>Mon, 24 Feb 2020 22:42:46 +0000</pubDate>
      <link>https://forem.com/landonmarder/elixir-s-pattern-matching-and-exunit-19k4</link>
      <guid>https://forem.com/landonmarder/elixir-s-pattern-matching-and-exunit-19k4</guid>
      <description>&lt;p&gt;While writing tests for a Phoenix app, I found something very strange.&lt;br&gt;
Look at the code below (contrived tests for this example):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="s2"&gt;"the solution to 2 + 2"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="s2"&gt;"the solution to 2 + 2"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;$ mix test&lt;/code&gt; in my terminal, I got 2 tests, 0 failures. Strange! Did I write my assertions correctly? Yes I did, but I should be getting 2 tests, 1 failure. Why am I getting them both to pass?&lt;/p&gt;

&lt;p&gt;This is Elixir's pattern matching at work! Looking at &lt;a href="https://github.com/elixir-lang/elixir/blob/v1.2.2/lib/ex_unit/lib/ex_unit/case.ex#L218"&gt;ExUnit.Case&lt;br&gt;
Module's code&lt;/a&gt;, we define a &lt;code&gt;test&lt;/code&gt; with a string. In the examples above, both test cases have the same string. Therefore, we will never match the second clause because the strings are the same. Because the first clause gets matched, and the first test passes, both tests will pass.&lt;/p&gt;

&lt;p&gt;When I further examine the output of my tests, I can confirm this because I see that I get the warning &lt;code&gt;test/models/user_group_test.exs:23: warning: this clause cannot match because a previous clause at line 19 always matches&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To get both tests to work as expected, I will need to make my test strings&lt;br&gt;
unique.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="s2"&gt;"the solution to 2 + 2"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="s2"&gt;"the solution to 2 + 2 is not this"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;2 tests, 1 failure!&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
