Aug 26, 2015
Table of contents:
Test Driven Development is a big part of the philosophy of the Ruby community.
Testing is an essential tool in the modern developer’s toolkit. A good test suite will save your arse and it will give you confidence to be constantly shipping new code code to production.
There are basically two schools of thought when it comes to testing in the Ruby community, Mini Test verses RSpec.
This week we’re going to be looking at using Mini Test, and next week we will be looking at using RSpec.
To be honest it doesn’t really make a difference which one you use. They are basically both the same because the outcome should achieve the same goals.
You should choose whichever you feel the most comfortable with, or whatever has been established as the chosen one in the project you are working on.
In either case, the only thing that really matters is that you are actually writing tests for your code!
To show you how to use Mini Test I’m going to be using the Todo
Gem that we created in last week’s tutorial, Creating a Ruby Gem.
If you aren’t familiar with creating Ruby Gems, I would recommend that you go back and read that tutorial first.
First we need to add minitest
as a development dependency of the project. Add the following line to todo.gemspec
:
spec.add_development_dependency 'minitest'
Next, in terminal, run the following command to pull in Mini Test:
$ bundle
You should see output that lists the Gems installed including Mini Test.
A couple of weeks ago we looked at using Rake (Understanding and Using Ruby Rake).
To make running the tests easier we can create a Rake task to handle it for us.
Copy the following code into your Rakefile
:
require 'rake/testtask'
Rake::TestTask.new do |task|
task.libs << %w[test lib]
task.pattern = 'test/test_*.rb'
end
task default: :test
Now you can simple run the following command to run your tests:
$ rake
However if you were to do that now nothing would happen as we have no tests yet!
Next we need to create a Test Helper file that will make writing the individual test files easier.
Create a new directory called test
and under that create a new file called test_helper.rb
:
require 'minitest/autorun'
require 'todo'
class Todo::Test < MiniTest::Test
end
Next if you run the rake
command again you should see some output.
Now that we’ve got Mini Test set up we can go ahead and write the first test.
Create a new file called test_task.rb
under the test
directory:
require 'test_helper'
class TestTask < Todo::Test
end
Next we can write the first test. In this test I’m simply asserting that a Task object can have title:
class TestTask < Todo::Test
def test_task_can_have_title
task = Todo::Task.new('Take out rubbish')
assert_equal 'Take out rubbish', task.title
end
end
This task will fail because we have not created the Task
class yet.
To make the test pass we can add the Task
class to the Todo
module under the lib
directory:
module Todo
class Task
attr_reader :title
def initialize(title)
@title = title
end
end
end
Now if you run the test again you should see it pass.
Mini Test comes with a whole load of other assertions for writing unit tests.
We’ve already seen the assert_equal
assertion that accepts two arguments and asserts that they are equal.
The opposite of that assertion would be refute_equal
, which takes two arguments and asserts that they are not equal.
Covering each assertion is out of the scope of this article, and to be honest you don’t need to memorise them all.
For a reference of the Mini Test assertions, take a look at: the documentation.
Mini Test also provides Spec helpers for writing your test using the Spec syntax style.
For example, you could write the test from earlier like this:
describe 'Task', 'Describe the Task object' do
let(:task) { Todo::Task.new('Take out rubbish') }
it 'should have a title' do
task.title.must_equal 'Take out rubbish'
end
end
The only difference is how you prefer to write your tests, it’s still the same outcome either way.
Mini Test is a very easy to use testing framework that is widely adopted in the Ruby community.
Mini Test is also the default testing framework of Ruby on Rails.
Writing tests is an important part of application development. It guides the development of your code, it will protect you against regressions, it will give you confidence to regularly ship applications to product, it will make collaboration easier, and it will provide documentation for your code.
Testing is an important practice to adopt, and Ruby is a great language to learn how to do it in.
Next week we will be looking at RSpec.