This is something that I just used, and found it surprisingly not straight-forward (or documented!) afaict.
So, I thought I’d share it here.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails", branch: "main"
gem "sqlite3"
end
require 'rails'
require 'active_record/railtie'
require "minitest/autorun"
require "logger"
class TestApp < Rails::Application
config.load_defaults "7.1"
#config.active_record.run_after_transaction_callbacks_in_order_defined = true
config.logger = Logger.new(STDOUT)
end
ENV["DATABASE_URL"] = "sqlite3::memory:"
Rails.application.initialize!
ActiveRecord::Schema.define do
create_table :books, force: true do |t|
# add some fields to the `books` table
end
end
class Book < ActiveRecord::Base
# define some custom behavior to the `Book` model
end
class BugTest < Minitest::Test
def test_it_does_a_thing
b = Book.new
assert_not book.persisted?
end
end
Ignoring the test case, I just found the order in which these things are defined to be particularly sensitive, and I hadn’t seen an example (that I can remember) where defaults are being loaded. Usually I see test cases where the author manipulates that value directly in the test block.
Do you think this is useful? Or do you prefer to edit the flag inside the test?
I’m also wondering what config options are loaded prior to load_defaults