Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spec for namespaced fixtures #2721

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"