Claude
Skills
Sign in
Back

minitest

Included with Lifetime
$97 forever

This skill should be used when the user asks about "Minitest", "Rails testing", "test/", "fixtures", "ActiveSupport::TestCase", "ActionDispatch::IntegrationTest", "system tests", "functional tests", "assert", "assert_equal", "assert_difference", "test_helper", "rails test", or needs guidance on testing Rails applications with the default Minitest framework.

Code Review

What this skill does


# Minitest for Rails

Comprehensive guide to testing Rails applications with Minitest, the default Rails testing framework.

## Test Case Classes

Rails provides specialized test case base classes:

| Base Class | Purpose | Location |
|------------|---------|----------|
| `ActiveSupport::TestCase` | Model and unit tests | `test/models/` |
| `ActionDispatch::IntegrationTest` | Multi-controller workflow tests | `test/integration/` |
| `ActionDispatch::SystemTestCase` | Browser-based end-to-end tests | `test/system/` |
| `ActionController::TestCase` | Functional controller tests | `test/controllers/` |
| `ActionView::TestCase` | View and helper tests | `test/helpers/` |
| `ActionMailer::TestCase` | Mailer tests | `test/mailers/` |
| `ActiveJob::TestCase` | Background job tests | `test/jobs/` |

## Configuration

```ruby
# test/test_helper.rb
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"

class ActiveSupport::TestCase
  # Run tests in parallel
  parallelize(workers: :number_of_processors)

  # Load fixtures
  fixtures :all

  # Add helper methods here
end


```

## Directory Structure

```
test/
├── controllers/          # Functional controller tests
├── fixtures/
│   ├── files/           # Upload test files
│   ├── users.yml
│   └── articles.yml
├── helpers/             # View helper tests
├── integration/         # Multi-controller tests
├── jobs/                # ActiveJob tests
├── mailers/             # Email tests
├── models/              # Model unit tests
├── system/              # Browser tests (Capybara)
├── application_system_test_case.rb
└── test_helper.rb
```

## Model Tests

```ruby
# test/models/article_test.rb
require "test_helper"

class ArticleTest < ActiveSupport::TestCase
  # Setup runs before each test
  setup do
    @article = articles(:published)
  end

  # Associations
  test "belongs to user" do
    assert_respond_to @article, :user
    assert_instance_of User, @article.user
  end

  test "has many comments" do
    assert_respond_to @article, :comments
  end

  # Validations
  test "is invalid without title" do
    article = Article.new(body: "Content", user: @user)
    assert_not article.valid?
    assert_includes article.errors[:title], "can't be blank"
  end

  test "is invalid with title over 255 characters" do
    article = Article.new(title: "a" * 256, body: "Content", user: @user)
    assert_not article.valid?
  end

  test "is valid with all attributes" do
    article = Article.new(title: "Test", body: "Content", user: @user)
    assert article.valid?
  end

  # Scopes
  test ".published returns only published articles" do
    assert_includes Article.published, articles(:published)
    assert_not_includes Article.published, articles(:draft)
  end

  test ".recent orders by created_at descending" do
    recent = Article.recent.first
    assert_equal articles(:published), recent
  end

  # Instance methods
  test "#publish! changes status to published" do
    article = articles(:draft)
    article.publish!

    assert_equal "published", article.status
    assert_not_nil article.published_at
  end

  test "#published? returns true for published articles" do
    assert articles(:published).published?
    assert_not articles(:draft).published?
  end
end
```

## Controller Tests (Functional)

```ruby
# test/controllers/articles_controller_test.rb
require "test_helper"

class ArticlesControllerTest < ActionDispatch::IntegrationTest
  setup do
    @article = articles(:published)
    @user = users(:one)
  end

  test "should get index" do
    get articles_url
    assert_response :success
  end

  test "should get show" do
    get article_url(@article)
    assert_response :success
  end

  test "should get new" do
    get new_article_url
    assert_response :success
  end

  test "should create article" do
    assert_difference("Article.count") do
      post articles_url, params: {
        article: { title: "New Article", body: "Content" }
      }
    end

    assert_redirected_to article_url(Article.last)
  end

  test "should not create article with invalid params" do
    assert_no_difference("Article.count") do
      post articles_url, params: { article: { title: "" } }
    end

    assert_response :unprocessable_entity
  end

  test "should update article" do
    patch article_url(@article), params: {
      article: { title: "Updated Title" }
    }

    assert_redirected_to article_url(@article)
    @article.reload
    assert_equal "Updated Title", @article.title
  end

  test "should destroy article" do
    assert_difference("Article.count", -1) do
      delete article_url(@article)
    end

    assert_redirected_to articles_url
  end
end
```

## Integration Tests

```ruby
# test/integration/user_flows_test.rb
require "test_helper"

class UserFlowsTest < ActionDispatch::IntegrationTest
  test "user can create article" do
    get new_article_url
    assert_response :success

    post articles_url, params: {
      article: { title: "My First Article", body: "Content" }
    }
    follow_redirect!
    assert_select "h1", "My First Article"
  end

  test "browsing articles as guest" do
    get articles_url
    assert_response :success
    assert_select "article", minimum: 1

    get article_url(articles(:published))
    assert_response :success
    assert_select "h1", articles(:published).title
  end
end
```

## System Tests

```ruby
# test/application_system_test_case.rb
require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
end
```

```ruby
# test/system/articles_test.rb
require "application_system_test_case"

class ArticlesTest < ApplicationSystemTestCase
  setup do
    @article = articles(:published)
  end

  test "visiting the index" do
    visit articles_url
    assert_selector "h1", text: "Articles"
  end

  test "creating an article" do
    visit new_article_url

    fill_in "Title", with: "System Test Article"
    fill_in "Body", with: "This is test content."
    click_on "Create Article"

    assert_text "Article was successfully created"
    assert_text "System Test Article"
  end

  test "updating an article" do
    visit article_url(@article)
    click_on "Edit"

    fill_in "Title", with: "Updated Title"
    click_on "Update Article"

    assert_text "Article was successfully updated"
    assert_text "Updated Title"
  end

  test "destroying an article" do
    visit article_url(@article)
    click_on "Delete", match: :first

    assert_text "Article was successfully destroyed"
  end

  test "adding comment with Turbo" do
    visit article_url(@article)

    fill_in "comment_body", with: "Great article!"
    click_on "Add Comment"

    within "#comments" do
      assert_text "Great article!"
    end
  end
end
```

## Fixtures

```yaml
# test/fixtures/users.yml
one:
  email: [email protected]
  name: Test User

admin:
  email: [email protected]
  name: Admin User
  role: admin

# test/fixtures/articles.yml
published:
  title: Published Article
  body: This is published content.
  status: published
  user: one
  published_at: <%= 1.day.ago %>
  created_at: <%= 2.days.ago %>

draft:
  title: Draft Article
  body: This is draft content.
  status: draft
  user: one
  created_at: <%= 1.day.ago %>

# With associations
# test/fixtures/comments.yml
one:
  body: Great article!
  article: published
  user: one
```

## Assertions Reference

### Basic Assertions

```ruby
assert value                        # truthy
assert_not value                    # falsy (Rails)
refute value                        # falsy (Minitest)

assert_equal expected, actual       # ==
assert_not_equal unexpected, actual
assert_same expected, actual        # same object
assert_nil value
assert_not_nil value

assert_empty collection
assert_not_empty collection
assert_includes collection, item
assert_not_includes collection, item

assert_instance_of Class, object
assert_kind_of Class, object
assert_res

Related in Code Review