Intro to TDD with Ruby's minitest
See also: https://semaphoreci.com/community/tutorials/getting-started-with-minitest
Requisites
We're going to implement a small unicorn class with these requirements:
- unicorns have names
- they are usually white, but can be any color
- they say "sparkly" things (don't worry, we'll explain what this means).
Setup
This will install minitest globally:
gem install minitest
Creating projects dir/files:
mkdir unicorn
cd unicorn
touch unicorn_test.rb
touch unicorn.rb
unicorn_test.rb
:
# configuring minitest:
gem 'minitest', '~> 5.0'
require 'minitest/autorun'
require 'minitest/pride' # colors! :)
# loading the class we want to test
require_relative 'unicorn'
# starting the test suite
class UnicornTest < Minitest::Test
end
To run the test:
ruby unicorn_test.rb
Assertions
assert_equal(expected, actual, msg = nil)
assert(test, msg = nil)
- use it instead of
assert_equal true, test
- use it instead of
refute(test, msg = nil)
- use it instead of
assert_equal false, test
- use it instead of
Skipping a Test
A test can be skipped with skip
.
Example:
def test_something_else
skip
assert_equal 1, nil - 1
end