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:

Press + to interact
require "test_helper"
class TaskTest < ActiveSupport::TestCase
test "a completed task is complete" do
task = Task.new
refute(task.complete?)
task.mark_completed
assert(task.complete?)
end
test "an uncompleted task does not count toward velocity" do
task = Task.new(size: 3)
refute(task.part_of_velocity?)
assert_equal(0, task.points_toward_velocity)
end
test "a task completed long ago does not count toward velocity" do
task = Task.new(size: 3)
task.mark_completed(6.months.ago)
refute(task.part_of_velocity?)
assert_equal(0, task.points_toward_velocity)
end
test "a task completed recently counts toward velocity" do
task = Task.new(size: 3)
task.mark_completed(1.day.ago)
assert(task.part_of_velocity?)
assert_equal(3, task.points_toward_velocity)
end
end

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. ...