Codementor Events

Why MiniTest is way better than RSpec

Published Mar 23, 2019
Why MiniTest is way better than RSpec

Simplicity

When I started programming profesionally I used to write Python and I always envied Ruby's testing framework RSpec when I watched RubyConf/RailsConf videos but at that time I wrote tests like below

def test_that_it_returns_a_list_of_authors():
    authors = get_authors_with_names_that_start_with('M')
    assert isinstance(authors, list)


def test_that_it_returns_a_list_of_books():
    books = search_for_books_by_author('Damian Marrett')
    assert isinstance(books, list)


def test_that_it_returns_an_html_element():
    URL = 'https://en.wikipedia.org/wiki/List_of_authors_by_name:_M'
    response = requests.get(URL)
    assert isinstance(create_html_element_from(response), HtmlElement)

As you can see I didn't know what testing and I was just excited about testing ath the moment. After I moved to Ruby and Ruby On Rails in past years annd started writing specs and tests for apps I realized that there's a big difference between MiniTest and RSpec, Rspec is like Perl there's always many ways to do things but MiniTest is just Ruby syntax not some crazy metaprogramming magic under the hood.

cloc-minitest-rspec.png

Learning curve

Just learend Ruby? Good no need to learn a new framework it's just Ruby's syntax + new methods and actually that's one of the reasons Google invented a simple and stupid language called Golang instead of a magical one to prevent developers from doing magic and just shipping features instead of playing with DSLs.

RSpec.describe Foo, type: :model do
  include_context 'sync something'

  describe 'associations' do
    it { is_expected.to belong_to(:bar) }
    it { is_expected.to belong_to(:baz).optional }
    it { is_expected.to belong_to(:user).optional }
    it { is_expected.to belong_to(:category).class_name('Something').optional }
  end
end

What is include_context? What is is_expected? How can I identify SUT (System under test) when I cannot see it? Why should I care an object internals like class name class_name?

class FooTest < MiniTest::Test
  def setup
    @foo = Foo.new
    @foo.sync_with_api
  end

  def test_should_belong_to_bar
    assert @foo.bar.present?
  end

  def test_should_belong_to_baz
    assert @foo.baz.present?
  end

  def test_should_belong_to_baz
    assert @foo.baz.present?
  end

  def test_should_belong_to_category
    assert @foo.categories.present?
  end
end

Hmm? bootstraping a simple object, syncing with some API and checking if there are some assocaitions, make sense.

No need to set bounderies for your tests 'cause your collages cannot go wrong with MiniTest or use a opiniated way to write tests, JUST SIMPLE RUBY.

Hope you like it šŸ•

Discover and read more posts from Alireza Bashiri
get started
post commentsBe the first to share your opinion
Alireza Bashiri
5 years ago

I cannot edit this post and Iā€™m not sure why so bear with my grammar issues :)

Show more replies