| name | Minitest & TDD Best Practices |
| description | This skill should be used when the user asks about "Minitest", "TDD", "test-driven development", "Rails testing", "writing tests", "test coverage", or testing strategies for Rails applications. Load this skill when implementing or reviewing tests. |
| version | 0.1.0 |
Minitest & TDD Best Practices
Minitest is Rails' default testing framework. This skill covers TDD workflow, Minitest patterns, and testing best practices for Rails applications.
TDD Workflow: Red, Green, Refactor
Red: Write failing test Green: Write minimum code to pass Refactor: Improve code with safety net
Test Types
Model Tests (Unit):
class OrderTest < ActiveSupport::TestCase
test "calculates total correctly" do
order = orders(:standard)
assert_equal 100.0, order.total
end
end
Controller Tests (Functional):
class PostsControllerTest < ActionDispatch::IntegrationTest
test "GET index returns success" do
get posts_path
assert_response :success
end
end
Integration Tests (System):
class UserFlowsTest < ActionDispatch::IntegrationTest
test "user can create post" do
post users_path, params: { user: { email: "test@example.com" } }
post posts_path, params: { post: { title: "Test" } }
assert_equal "Test", Post.last.title
end
end
Minitest Assertions
assert_equal expected, actual
assert_not_equal not_expected, actual
assert condition
assert_not condition
assert_nil value
assert_includes collection, item
assert_raises ExceptionClass do
# code
end
assert_difference "Model.count", 1 do
# code
end
Fixtures
# test/fixtures/posts.yml
one:
title: "First Post"
author: steve
published: true
test "can access fixture" do
post = posts(:one)
assert_equal "First Post", post.title
end
What NOT to Test
❌ Framework behavior (ActiveRecord saves, validations work) ❌ Third-party libraries ❌ Simple getters/setters
✅ Business logic ✅ Custom validations ✅ Complex workflows ✅ Edge cases
Best Practices
- One assertion per test (when possible)
- Clear test names
- Fast tests (< 1 second each)
- Independent tests (no shared state)
- DRY tests (extract common setup)
Test your code, not the framework.
Additional Resources
Example Files
Working examples in examples/:
examples/model_test_example.rb- Comprehensive model test demonstrating validations, associations, business logic, edge cases, state transitions, scopes, and callbacks