Search⌘ K
AI Features

Minitest Basics

Explore Minitest fundamentals for Rails testing by learning its syntax, assertion methods like assert and refute, and ActiveSupport's testing extensions. Understand how to write clear tests for models and use Rails-specific shortcuts to simplify testing processes.

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:

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