minitest-rails

repository·master·Indexed 20 days ago

https://github.com/minitest/minitest-rails

Integration for Minitest within Rails that enables the Minitest::Spec DSL within standard Rails TestCase classes. It provides Rails generators for models and scaffolds, automatic base class selection via spec types (such as :model, :job, and :mailer), and specialized expectations for ActionCable and ActiveSupport, including must_change and must_differ.

Tokens
3.5K
Snippets
20
Records
21
Agent score
69%

What's inside minitest-rails

  1. Use must_differ instead of must_change for Rails 5.1+ compatibility

    master

    In newer versions of minitest-rails, the expectation for checking changes has been updated to align with Rails' assert_changes assertion.

    • Use must_differ to wrap the assert_changes assertion (standard for Rails 5.1+).
    • The older must_change expectation, which previously referred to assert_difference, is no longer the primary way to check for changes in this context.
    # Use must_differ for modern Rails change assertions
    subject.must_differ do
      # code that causes a change
    end
  2. Initialize minitest-rails in a Rails app

    master

    After adding the gem to your Gemfile and running bundle install, run the installation generator to set up the necessary test infrastructure. This will create a test_helper.rb file in your test/ directory.

    rails generate minitest:install
  3. Use constants instead of strings in describe blocks

    master

    When using the Spec DSL, pass the actual class/constant to the describe block rather than its name as a string. If you must use a string, you must explicitly provide the :controller symbol as an additional argument to specify the test class type.

    Correct usage:

    describe WidgetsController do

    Incorrect usage:

    describe "WidgetsController" do

    Usage with string name:

    describe "WidgetsController", :controller do
  4. Use Minitest::Spec DSL with Rails generators

    master

    By default, minitest-rails enables the Minitest::Spec DSL within Rails TestCase classes (like ActiveSupport::TestCase). When you use standard Rails generators (model, controller, etc.), they will generate tests using the Spec DSL.

    Generator Options

    • Disable Spec DSL: If you want to generate tests without the Minitest::Spec DSL, use the --no-spec flag.
    • Skip Fixtures: To generate a model without a corresponding fixture file, use the --skip-fixture flag.

    Setting Defaults

    You can configure your Rails application to always use standard Minitest (without Spec DSL) and skip fixtures by adding the following to config/application.rb:

    config.generators do |g|
      g.test_framework :minitest, spec: false, fixture: false
    end
    # Example: Generating a model with Spec DSL (default)
    rails generate model User
    
    # Example: Generating a model without Spec DSL
    rails generate model User --no-spec
    
    # Example: Generating a model without a fixture
    rails generate model User --skip-fixture
  5. Automatic Base Class Selection via Spec Types

    master

    Minitest-rails uses register_spec_type to automatically select the correct Rails TestCase base class based on how you describe your tests. You can trigger this in two ways:

    1. By Class Inheritance: If the class provided to describe inherits from a specific Rails component (e.g., ActiveRecord::Base), the correct base class is used.
    2. By Tag/Symbol: You can explicitly pass symbols to the describe block to force a specific base class.

    Supported mappings include:

    SymbolBase ClassComponent
    :modelActiveSupport::TestCaseActiveRecord / Models
    :view or :helperActionView::TestCaseActionView / Helpers
    :jobActiveJob::TestCaseActiveJob
    :mailerActionMailer::TestCaseActionMailer
    :integrationActionDispatch::IntegrationTestActionDispatch / Integration
    :generatorRails::Generators::TestCaseRails Generators
    :channelActionCable::Channel::TestCaseActionCable Channels
    :connectionActionCable::Connection::TestCaseActionCable Connections
    # Using symbols to specify the base class
    describe "My Mailer", :mailer do
      it "sends an email" do
        # ...
      end
    end
    
    # Using symbols for integration tests
    describe "User Flow", :integration do
      # ...
    end
  6. Use the Minitest Spec DSL with Rails

    master

    When minitest-rails is loaded, it extends ActiveSupport::TestCase with the Minitest::Spec::DSL. This allows you to write tests using the describe/it syntax instead of standard class-based unit tests. Additionally, it includes ActiveSupport::Testing::ConstantLookup, which allows the spec DSL to resolve constants from the test name.

    # Example of using the spec DSL in a Rails environment
    describe User do
      it "is valid with a name" do
        user = User.new(name: "Alice")
        assert user.valid?
      end
    end
  7. Configure minitest-rails version for your Rails version

    master

    The minitest-rails gem follows Rails versioning. You should specify a version in your Gemfile that matches your current Rails version to ensure compatibility.

    Examples:

    • For Rails 8.1: gem "minitest-rails", "~> 8.1.0"
    • For Rails 7.2: gem "minitest-rails", "~> 7.2.0"
    • For Rails 6.0: gem "minitest-rails", "~> 6.0.0"
    gem "minitest-rails", "~> 8.1.0"
  8. Install minitest-rails using the Rails generator

    master

    You can use the minitest:install Rails generator to set up the necessary directory structure and configuration files for minitest-rails in your application. This generator creates standard test directories (fixtures, controllers, mailers, models, helpers, integration) and populates test/test_helper.rb, test/application_system_test_case.rb, and test/channels/application_cable/connection_test.rb.

    To skip the creation of Active Record related files, use the --skip_active_record flag.

    rails generate minitest:install
    
    # To skip Active Record files:
    rails generate minitest:install --skip_active_record