diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index b5909f885..b6727d71b 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 diff --git a/spec/rspec/rails/fixture_support_spec.rb b/spec/rspec/rails/fixture_support_spec.rb index f14a1dfa3..c6a9c9f75 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 92f5bafa6..ed1b0ad4d 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,16 @@ class AssociatedModel < ActiveRecord::Base class AlternatePrimaryKeyModel < ActiveRecord::Base self.primary_key = :my_id extend Connections + 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 000000000..3ee371201 --- /dev/null +++ b/spec/support/fixtures/namespaced/model.yml @@ -0,0 +1,2 @@ +one: + name: "Model #1"