From cf2ec15f6f7b186367e57a93b74812afd2983681 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sun, 10 Dec 2023 22:12:22 +0000 Subject: [PATCH 1/3] Refactor active_record class creation --- spec/support/ar_classes.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/spec/support/ar_classes.rb b/spec/support/ar_classes.rb index 92f5bafa6d..daa4b05fdc 100644 --- a/spec/support/ar_classes.rb +++ b/spec/support/ar_classes.rb @@ -5,13 +5,13 @@ module Connections def self.extended(host) + fields = + { host.primary_key => "integer PRIMARY KEY AUTOINCREMENT" } + + fields.merge!(host.connection_fields) if host.respond_to?(:connection_fields) + host.connection.execute <<-EOSQL - CREATE TABLE #{host.table_name} ( - #{host.primary_key} integer PRIMARY KEY AUTOINCREMENT, - associated_model_id integer, - mockable_model_id integer, - nonexistent_model_id integer - ) + CREATE TABLE #{host.table_name} ( #{fields.map { |column, type| "#{column} #{type}"}.join(", ") }) EOSQL host.reset_column_information @@ -24,7 +24,11 @@ class NonActiveRecordModel end class MockableModel < ActiveRecord::Base + def self.connection_fields + { associated_model_id: :integer } + end extend Connections + has_one :associated_model end @@ -32,7 +36,11 @@ class SubMockableModel < MockableModel end class AssociatedModel < ActiveRecord::Base + def self.connection_fields + { mockable_model_id: :integer, nonexistent_model_id: :integer } + end extend Connections + belongs_to :mockable_model belongs_to :nonexistent_model, class_name: "Other" end @@ -40,5 +48,6 @@ class AssociatedModel < ActiveRecord::Base class AlternatePrimaryKeyModel < ActiveRecord::Base self.primary_key = :my_id extend Connections + attr_accessor :my_id end From bbebd2801d99417fa11ec2fc8f7711e9a8a4c195 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sun, 10 Dec 2023 22:37:26 +0000 Subject: [PATCH 2/3] Add spec for namespaced fixtures --- spec/rspec/rails/fixture_support_spec.rb | 30 +++++++++++++++++----- spec/support/ar_classes.rb | 10 ++++++++ spec/support/fixtures/namespaced/model.yml | 2 ++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 spec/support/fixtures/namespaced/model.yml diff --git a/spec/rspec/rails/fixture_support_spec.rb b/spec/rspec/rails/fixture_support_spec.rb index f14a1dfa30..c6a9c9f751 100644 --- a/spec/rspec/rails/fixture_support_spec.rb +++ b/spec/rspec/rails/fixture_support_spec.rb @@ -37,12 +37,6 @@ module RSpec::Rails expect_to_pass(group) end - - def expect_to_pass(group) - result = group.run(failure_reporter) - failure_reporter.exceptions.map { |e| raise e } - expect(result).to be true - end end it "will allow #setup_fixture to run successfully" do @@ -54,5 +48,29 @@ def expect_to_pass(group) expect { group.new.setup_fixtures }.to_not raise_error end + + it "handles namespaced fixtures" do + group = RSpec::Core::ExampleGroup.describe do + include FixtureSupport + fixtures 'namespaced/model' + + it 'has the fixture' do + namespaced_model(:one) + end + end + if Rails.version.to_f >= 7.1 + group.fixture_paths = [File.expand_path('../../support/fixtures', __dir__)] + else + group.fixture_path = File.expand_path('../../support/fixtures', __dir__) + end + + expect_to_pass(group) + end + + def expect_to_pass(group) + result = group.run(failure_reporter) + failure_reporter.exceptions.map { |e| raise e } + expect(result).to be true + end end end diff --git a/spec/support/ar_classes.rb b/spec/support/ar_classes.rb index daa4b05fdc..ed1b0ad4de 100644 --- a/spec/support/ar_classes.rb +++ b/spec/support/ar_classes.rb @@ -51,3 +51,13 @@ class AlternatePrimaryKeyModel < ActiveRecord::Base attr_accessor :my_id end + +module Namespaced + class Model < ActiveRecord::Base + def self.connection_fields + { name: :string } + end + + extend Connections + end +end diff --git a/spec/support/fixtures/namespaced/model.yml b/spec/support/fixtures/namespaced/model.yml new file mode 100644 index 0000000000..3ee3712015 --- /dev/null +++ b/spec/support/fixtures/namespaced/model.yml @@ -0,0 +1,2 @@ +one: + name: "Model #1" From 55022d866dd0d96bfbd197db8cfc09470a95752d Mon Sep 17 00:00:00 2001 From: Benedikt Deicke Date: Fri, 24 Nov 2023 10:41:10 +0100 Subject: [PATCH 3/3] Fix support for namespaced fixtures with Rails 7.1 --- lib/rspec/rails/fixture_support.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index b5909f885a..b6727d71b3 100644 --- a/lib/rspec/rails/fixture_support.rb +++ b/lib/rspec/rails/fixture_support.rb @@ -43,14 +43,14 @@ module Fixtures if ::Rails.version.to_f >= 7.1 def fixtures(*args) super.tap do - fixture_sets.each_key do |fixture_name| - proxy_method_warning_if_called_in_before_context_scope(fixture_name) + fixture_sets.each_pair do |method_name, fixture_name| + proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name) end end end - def proxy_method_warning_if_called_in_before_context_scope(fixture_name) - define_method(fixture_name) do |*args, **kwargs, &blk| + def proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name) + define_method(method_name) do |*args, **kwargs, &blk| if RSpec.current_scope == :before_context_hook RSpec.warn_with("Calling fixture method in before :context ") else