<?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: Andrea Salicetti</title>
    <description>The latest articles on DEV Community by Andrea Salicetti (@knightq).</description>
    <link>https://dev.to/knightq</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%2F220531%2F6c1ed4ef-43ff-4d78-9a3b-901ab4bc89b5.png</url>
      <title>DEV Community: Andrea Salicetti</title>
      <link>https://dev.to/knightq</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/knightq"/>
    <language>en</language>
    <item>
      <title>Insane ruby testing</title>
      <dc:creator>Andrea Salicetti</dc:creator>
      <pubDate>Mon, 07 Dec 2020 16:48:47 +0000</pubDate>
      <link>https://dev.to/knightq/insane-ruby-testing-3g18</link>
      <guid>https://dev.to/knightq/insane-ruby-testing-3g18</guid>
      <description>&lt;p&gt;A couple of weeks ago, I came up this post on twitter:&lt;br&gt;
&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://twitter.com/hashtag/Ruby?src=hash&amp;amp;ref_src=twsrc%5Etfw"&gt;#Ruby&lt;/a&gt; when you think you've seen everything in Ruby &lt;a href="https://t.co/6Q7m9kOcwV"&gt;pic.twitter.com/6Q7m9kOcwV&lt;/a&gt;&lt;/p&gt;— Konrad Oleksiuk (@koleksiuk) &lt;a href="https://twitter.com/koleksiuk/status/1317073715595780097?ref_src=twsrc%5Etfw"&gt;October 16, 2020&lt;/a&gt;
&lt;/blockquote&gt; &lt;br&gt;
Just after reading that piece of code, in my mind two contrasting thoughts appeared: &lt;strong&gt;clever&lt;/strong&gt; &amp;amp; &lt;strong&gt;evil&lt;/strong&gt;.

&lt;p&gt;The correct term to represent my state of mind about that is: &lt;em&gt;sublime&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Sublime (from the Latin &lt;em&gt;sublimis&lt;/em&gt;, or in the variant &lt;em&gt;sublimus&lt;/em&gt;, composed of &lt;em&gt;sub-&lt;/em&gt;, "below", and &lt;em&gt;limen&lt;/em&gt;, "threshold"; therefore properly: &lt;em&gt;"what is at the limit"&lt;/em&gt;, or of &lt;em&gt;sub-&lt;/em&gt;, "below" , and &lt;em&gt;limen-&lt;/em&gt;, "threshold", properly &lt;em&gt;"that reaches below the highest threshold"&lt;/em&gt;) is an aesthetic category that dates back to classical antiquity and later to Romanticism.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;(source: &lt;a href="https://it.wikipedia.org/wiki/Sublime"&gt;Wikipedia&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
   😎 What's &lt;em&gt;clever&lt;/em&gt; about this?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It's the power and pervasiveness of ruby metaprogramming: everything in ruby, also what may appear as a simple "statement" of the language, in reality is a method call (+ syntactic sugar that makes the parenthesis optional).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It makes testing of private method as easy as the testing of public ones. Let's explain with a simple example.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:object_id&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="c1"&gt;# some public methods&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;

  &lt;span class="kp"&gt;private&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;object&lt;/span&gt;
    &lt;span class="vi"&gt;@object&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="n"&gt;object_class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="nb"&gt;object_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How would you test private method &lt;code&gt;object&lt;/code&gt; on &lt;code&gt;MyClass&lt;/code&gt;?&lt;br&gt;
Without that trick you would have four choices:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Make private method that need to be tested public&lt;/em&gt;.
Of course, this choice has no sense: the design of an API about what is public (= must be considered available to clients) and what is not, should not be conditioned by testing constraints.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Do not test private methods&lt;/em&gt;: someone may agree that's fine. I don't and I'll explain why in the next paragraph.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Exploit ruby's message passing feature&lt;/em&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'test_helper'&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClassTest&lt;/span&gt;
  &lt;span class="n"&gt;let!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="no"&gt;AClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:subject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="no"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_object&lt;/span&gt;
    &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stubs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:object_class&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;AClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;assert_equal&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# this sounds quite "awkward"...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  😈 What's &lt;em&gt;wrong&lt;/em&gt; (evil) about this?
&lt;/h2&gt;

&lt;p&gt;The question on where a purist of code and TDD may rise his finger is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Shall I really test &lt;em&gt;private&lt;/em&gt; methods?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The good Steven Solomon (&lt;a href="https://twitter.com/soonernotfaster"&gt;@soonernotfaster&lt;/a&gt;) has written an &lt;a href="https://soonernotfaster.com/posts/how-to-test-private-methods-in-ruby-and-rails/"&gt;excellent post&lt;/a&gt; about what should be considered &lt;em&gt;the right way&lt;/em&gt; to test private methods and I agree with him.&lt;br&gt;
So, my intention here is not to rewrite the same considerations, but just add my personal point of view about one simple fact: as I've written above, testing (especially legacy code), seldom, is a &lt;em&gt;path&lt;/em&gt; toward good code design.&lt;br&gt;
Of course, if you're doing right TDD, writing the code first will let you enjoy the beautyness of the so called "emerging design" it brings, and probably you'll never had to test private methods &lt;em&gt;directly&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  But
&lt;/h3&gt;

&lt;p&gt;If you daily deal with &lt;em&gt;legacy code&lt;/em&gt; you will often have to compromise between the &lt;em&gt;optimum&lt;/em&gt; and the &lt;em&gt;just works&lt;/em&gt; solution.&lt;br&gt;
In these choices, bring on a bad original design decision by keeping the code uncovered will just you take your codebase deeper under in the grave of technical debt that - one day or another one - will present its bill.&lt;br&gt;
One the other hand, the cost to fully refactor in just one shot the whole "spaghetti code" mud might be too hard to face and the temptation could be to say: "...well, I'll covert this with tests later, someday". That means: never.&lt;/p&gt;

&lt;p&gt;So what?&lt;/p&gt;

&lt;h2&gt;
  
  
  Shall I use it or not?
&lt;/h2&gt;

&lt;p&gt;Here we come in the land of the opinions about pattern and anti-pattern of software design.&lt;/p&gt;

&lt;p&gt;I think that in this case there is no &lt;em&gt;indisputable truth&lt;/em&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hfMd2pvM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pixabay.com/get/55e1d7444257a514f6da8c7dda3536781538dde05a547641_1280.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hfMd2pvM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pixabay.com/get/55e1d7444257a514f6da8c7dda3536781538dde05a547641_1280.jpg" alt="Path to perfection" width="880" height="494"&gt;&lt;/a&gt;&lt;br&gt;
Perfection is an &lt;a href="https://en.wikipedia.org/wiki/Asymptote"&gt;asymptote&lt;/a&gt;: you know where it is, it can be your goal, but you know you'll never be able to reach it (and I think it's fine so).&lt;br&gt;
Moreover, it's a &lt;em&gt;path&lt;/em&gt;, made by little steps: we should approach it in a way that bring us each day a step further towards it, without falling in the chasm of despair and resignation.&lt;/p&gt;

&lt;p&gt;In my little city, Bologna, we have a proverb, hard to translate in english: I'll try to do it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rather than nothing, it's worth "rather"...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or, paraphrasing: whatever you do to enhance something, is better than to do nothing at all.&lt;/p&gt;

&lt;p&gt;I guess (and hope) that, in our path to perfection, someday we'll look back at that code, thinking: "What a horrible piece of code I wrote: let's remove that private unless and refactor!"&lt;/p&gt;

&lt;p&gt;Don't blame yourself now, for writing what you already know that will be blamed (probably and hopefully) someday in the future: that day, will be a good day, since you will have grown, as developer, and you won't, if you've hadn't done that little, bad, useful step first.&lt;/p&gt;

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