Insane ruby testing
Tags: testing, ruby
Published: 2020-12-07 16:48:47

A couple of weeks ago, I came up this post on twitter:
<blockquote class="twitter-tweet"><p lang="en" dir="ltr"><a href="https://twitter.com/hashtag/Ruby?src=hash&amp;ref_src=twsrc%5Etfw">#Ruby</a> when you think you&#39;ve seen everything in Ruby <a href="https://t.co/6Q7m9kOcwV">pic.twitter.com/6Q7m9kOcwV</a></p>&mdash; Konrad Oleksiuk (@koleksiuk) <a href="https://twitter.com/koleksiuk/status/1317073715595780097?ref_src=twsrc%5Etfw">October 16, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
Just after reading that piece of code, in my mind two contrasting thoughts appeared: **clever** & **evil**.

The correct term to represent my state of mind about that is: *sublime*.

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

*(source: [Wikipedia](https://it.wikipedia.org/wiki/Sublime))*

## 😎 What's *clever* about this?

- 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).

- It makes testing of private method as easy as the testing of public ones. Let's explain with a simple example.
```ruby
class MyClass
  attr_accessor :object_id
  ...
  # some public methods
  ...

  private

  def object
    @object ||= object_class.find_by(id: object_id)
  end
end
```
How would you test private method `object` on `MyClass`?
Without that trick you would have four choices:

 1. *Make private method that need to be tested public*.
    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.
 2. *Do not test private methods*: someone may agree that's fine. I don't and I'll explain why in the next paragraph.
 3. *Exploit ruby's message passing feature*:
```ruby
require 'test_helper'

class MyClassTest
  let!(:obj) { AClass.create }
  let(:subject) { MyClass.new }

  def test_object
    subject.stubs(:object_class).returns(AClass)

    assert_equal obj, subject.send(:object) # this sounds quite "awkward"...
  end
end
```

## 😈 What's *wrong* (evil) about this?

The question on where a purist of code and TDD may rise his finger is:

> Shall I really test *private* methods?

The good Steven Solomon ([@soonernotfaster](https://twitter.com/soonernotfaster)) has written an [excellent post](https://soonernotfaster.com/posts/how-to-test-private-methods-in-ruby-and-rails/) about what should be considered *the right way* to test private methods and I agree with him.
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 *path* toward good code design.
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 *directly*.

### But

If you daily deal with *legacy code* you will often have to compromise between the *optimum* and the *just works* solution.
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.
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.

So what?

## Shall I use it or not?

Here we come in the land of the opinions about pattern and anti-pattern of software design.

I think that in this case there is no *indisputable truth*.
![Path to perfection](https://pixabay.com/get/55e1d7444257a514f6da8c7dda3536781538dde05a547641_1280.jpg)
Perfection is an [asymptote](https://en.wikipedia.org/wiki/Asymptote): 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).
Moreover, it's a *path*, 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.

In my little city, Bologna, we have a proverb, hard to translate in english: I'll try to do it:

> Rather than nothing, it's worth "rather"...

or, paraphrasing: whatever you do to enhance something, is better than to do nothing at all.

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!"

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.