Skip to content

Commit 9447bd6

Browse files
committed
Merge pull request #2721 from rspec/add-namespaced-fixture-spec
Add spec for namespaced fixtures
1 parent 52182cd commit 9447bd6

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

lib/rspec/rails/fixture_support.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ module Fixtures
4343
if ::Rails.version.to_f >= 7.1
4444
def fixtures(*args)
4545
super.tap do
46-
fixture_sets.each_key do |fixture_name|
47-
proxy_method_warning_if_called_in_before_context_scope(fixture_name)
46+
fixture_sets.each_pair do |method_name, fixture_name|
47+
proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
4848
end
4949
end
5050
end
5151

52-
def proxy_method_warning_if_called_in_before_context_scope(fixture_name)
53-
define_method(fixture_name) do |*args, **kwargs, &blk|
52+
def proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
53+
define_method(method_name) do |*args, **kwargs, &blk|
5454
if RSpec.current_scope == :before_context_hook
5555
RSpec.warn_with("Calling fixture method in before :context ")
5656
else

spec/rspec/rails/fixture_support_spec.rb

+24-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ module RSpec::Rails
3737

3838
expect_to_pass(group)
3939
end
40-
41-
def expect_to_pass(group)
42-
result = group.run(failure_reporter)
43-
failure_reporter.exceptions.map { |e| raise e }
44-
expect(result).to be true
45-
end
4640
end
4741

4842
it "will allow #setup_fixture to run successfully" do
@@ -54,5 +48,29 @@ def expect_to_pass(group)
5448

5549
expect { group.new.setup_fixtures }.to_not raise_error
5650
end
51+
52+
it "handles namespaced fixtures" do
53+
group = RSpec::Core::ExampleGroup.describe do
54+
include FixtureSupport
55+
fixtures 'namespaced/model'
56+
57+
it 'has the fixture' do
58+
namespaced_model(:one)
59+
end
60+
end
61+
if Rails.version.to_f >= 7.1
62+
group.fixture_paths = [File.expand_path('../../support/fixtures', __dir__)]
63+
else
64+
group.fixture_path = File.expand_path('../../support/fixtures', __dir__)
65+
end
66+
67+
expect_to_pass(group)
68+
end
69+
70+
def expect_to_pass(group)
71+
result = group.run(failure_reporter)
72+
failure_reporter.exceptions.map { |e| raise e }
73+
expect(result).to be true
74+
end
5775
end
5876
end

spec/support/ar_classes.rb

+25-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
module Connections
77
def self.extended(host)
8+
fields =
9+
{ host.primary_key => "integer PRIMARY KEY AUTOINCREMENT" }
10+
11+
fields.merge!(host.connection_fields) if host.respond_to?(:connection_fields)
12+
813
host.connection.execute <<-EOSQL
9-
CREATE TABLE #{host.table_name} (
10-
#{host.primary_key} integer PRIMARY KEY AUTOINCREMENT,
11-
associated_model_id integer,
12-
mockable_model_id integer,
13-
nonexistent_model_id integer
14-
)
14+
CREATE TABLE #{host.table_name} ( #{fields.map { |column, type| "#{column} #{type}"}.join(", ") })
1515
EOSQL
1616

1717
host.reset_column_information
@@ -24,21 +24,40 @@ class NonActiveRecordModel
2424
end
2525

2626
class MockableModel < ActiveRecord::Base
27+
def self.connection_fields
28+
{ associated_model_id: :integer }
29+
end
2730
extend Connections
31+
2832
has_one :associated_model
2933
end
3034

3135
class SubMockableModel < MockableModel
3236
end
3337

3438
class AssociatedModel < ActiveRecord::Base
39+
def self.connection_fields
40+
{ mockable_model_id: :integer, nonexistent_model_id: :integer }
41+
end
3542
extend Connections
43+
3644
belongs_to :mockable_model
3745
belongs_to :nonexistent_model, class_name: "Other"
3846
end
3947

4048
class AlternatePrimaryKeyModel < ActiveRecord::Base
4149
self.primary_key = :my_id
4250
extend Connections
51+
4352
attr_accessor :my_id
4453
end
54+
55+
module Namespaced
56+
class Model < ActiveRecord::Base
57+
def self.connection_fields
58+
{ name: :string }
59+
end
60+
61+
extend Connections
62+
end
63+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
one:
2+
name: "Model #1"

0 commit comments

Comments
 (0)