Ruby:
Writing tests

How to:

Ruby comes with a built-in library called Test::Unit for writing unit tests, encapsulating testing practices within straightforward structures. However, the Ruby community often leans towards third-party libraries like RSpec and Minitest due to their enhanced expressiveness and flexibility.

Using Test::Unit:

require 'test/unit'

class CalculatorTest < Test::Unit::TestCase
  def test_addition
    result = 2 + 2
    assert_equal 4, result
  end
end

Run your test file from the terminal, and you should get an output indicating success or failure of the tests:

Loaded suite test_calculator
Started
.
Finished in 0.001288 seconds.
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

Using RSpec:

RSpec is a popular BDD (Behavior-Driven Development) framework for Ruby. Install the gem with gem install rspec, then initialize it in your project with rspec --init.

# calculator_spec.rb
require_relative '../calculator'

describe Calculator do
  it 'correctly adds two numbers' do
    expect(Calculator.add(2, 2)).to eq(4)
  end
end

Run tests with the rspec command. Example output:

.

Finished in 0.002 seconds (files took 0.1 seconds to load)
1 example, 0 failures

Using Minitest:

Minitest provides a complete suite of testing facilities supporting TDD, BDD, mocking, and benchmarking. Install it with gem install minitest and use as follows:

# test_calculator.rb
require 'minitest/autorun'
require_relative '../calculator'

class CalculatorTest < Minitest::Test
  def test_addition
    assert_equal 4, Calculator.add(2, 2)
  end
end

Run your test file directly or through the rake task set up for minitest. Sample output:

Run options: --seed 33407

# Running:

.

Finished in 0.001027s, 974.5922 runs/s, 974.5922 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

By implementing tests in your Ruby projects using these libraries, you adhere to best practices, leading to more reliable and maintainable code bases.