Minitest Basics
Learn Minitest syntax, methods, and assertion methods and learn to write the first test on Minitest.
Minitest syntax
Our project’s test
directory contains Minitest equivalents of many of the RSpec tests we’ve written thus far.
Here’s an example, specifically, the tests for the Task
model:
require "test_helper"class TaskTest < ActiveSupport::TestCasetest "a completed task is complete" dotask = Task.newrefute(task.complete?)task.mark_completedassert(task.complete?)endtest "an uncompleted task does not count toward velocity" dotask = Task.new(size: 3)refute(task.part_of_velocity?)assert_equal(0, task.points_toward_velocity)endtest "a task completed long ago does not count toward velocity" dotask = Task.new(size: 3)task.mark_completed(6.months.ago)refute(task.part_of_velocity?)assert_equal(0, task.points_toward_velocity)endtest "a task completed recently counts toward velocity" dotask = Task.new(size: 3)task.mark_completed(1.day.ago)assert(task.part_of_velocity?)assert_equal(3, task.points_toward_velocity)endend
This looks broadly similar to the RSpec we’ve been looking at, but the syntax has some clear differences. Let’s take a look at the main ones.
On line 1 the file test_helper
is required. It contains Rails and application-related setup common to all Minitest files. It’s the file to which we just added Mocha.
Minitest methods
In Minitest, we can’t put a test method just anywhere or name it just anything; tests need to be methods of a subclass of the class Minitest::Test
. (The class ActiveSupport::TestCase
that the test uses is, in fact, a subclass of Minitest::Test
). In standard Minitest, a test method is any method whose name starts with test_
, as in ...