Skip to content

Commit

Permalink
Tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Senko committed Dec 22, 2023
1 parent 1299eb6 commit eddc9ba
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
2 changes: 2 additions & 0 deletions test/dummy/app/models/dummy_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class DummyModel < ApplicationRecord
end
2 changes: 2 additions & 0 deletions test/dummy/app/models/inherited_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class InheritedModel < DummyModel
end
2 changes: 2 additions & 0 deletions test/dummy/app/models/missing_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class MissingModel < ApplicationRecord
end
8 changes: 8 additions & 0 deletions test/dummy/db/migrate/20231222124254_create_dummy_models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateDummyModels < ActiveRecord::Migration[7.1]
def change
create_table :dummy_models do |t|

t.timestamps
end
end
end
19 changes: 19 additions & 0 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2023_12_22_124254) do
create_table "dummy_models", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
58 changes: 54 additions & 4 deletions test/rails_model_load_hook_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
require "test_helper"
require 'minitest/autorun'

class RailsModelLoadHookTest < ActiveSupport::TestCase
test "it has a version number" do
assert RailsModelLoadHook::VERSION
end
describe RailsModelLoadHook do
let(:loaded) { [] }
let(:on_load) { -> { loaded << _1 } }

before do
on_load = self.on_load

ActiveSupport.on_load :model_class do
on_load[self]
end
end

module SharedExamples
refine Minitest::Spec.singleton_class do
def passing
it 'passes the hook' do
_(loaded).must_include model
end
end

def skipping
it 'skips the hook' do
_(loaded).wont_include model
end
end
end
end

using SharedExamples

describe 'with existing models' do
let(:model) { DummyModel.tap { _1.new } }

passing
end

describe 'with inherited models' do
let(:model) { InheritedModel.tap { _1.new } }

passing
end

describe 'with ApplicationRecord' do
let(:model) { ApplicationRecord }

skipping
end

describe 'with models lacking DB tables' do
let(:model) { MissingModel }

skipping
end
end

0 comments on commit eddc9ba

Please sign in to comment.