Skip to content

Commit

Permalink
Merge pull request #2721 from rspec/add-namespaced-fixture-spec
Browse files Browse the repository at this point in the history
Add spec for namespaced fixtures
  • Loading branch information
JonRowe authored Dec 11, 2023
2 parents cbc6fcc + 55022d8 commit bbc6e62
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
8 changes: 4 additions & 4 deletions lib/rspec/rails/fixture_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 24 additions & 6 deletions spec/rspec/rails/fixture_support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
31 changes: 25 additions & 6 deletions spec/support/ar_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,21 +24,40 @@ class NonActiveRecordModel
end

class MockableModel < ActiveRecord::Base
def self.connection_fields
{ associated_model_id: :integer }
end
extend Connections

has_one :associated_model
end

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

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
2 changes: 2 additions & 0 deletions spec/support/fixtures/namespaced/model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
one:
name: "Model #1"

0 comments on commit bbc6e62

Please sign in to comment.