Skip to content

Commit 13812b9

Browse files
committed
Use conditional hooks for money precision
Move the `Money.default_infinite_precision` test setup from a shared context into global, conditional RSpec hooks. The previous `shared_context` approach, which relied on implicit metadata inclusion, is deprecated and was failing with the modern `shared_context_metadata_behavior = :apply_to_host_groups` setting. An explicit `include_context` is unsuitable because this behavior is sometimes required for individual examples (`it` blocks), where contexts cannot be included. This commit defines global `before` and `after` hooks in the RSpec configuration that are filtered by the `:default_infinite_precision_true` metadata tag. This provides a declarative and efficient way to apply the necessary setup and teardown to any example group or individual example without repetitive code, while aligning with modern RSpec practices. Close #1136
1 parent 4a4284d commit 13812b9

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

spec/spec_helper.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@
6262
# compatibility in RSpec 3). It causes shared context metadata to be
6363
# inherited by the metadata hash of host groups and examples, rather than
6464
# triggering implicit auto-inclusion in groups with matching metadata.
65-
# TODO: enable this option. Tracked in RubyMoney/money#1136
66-
# config.shared_context_metadata_behavior = :apply_to_host_groups
65+
config.shared_context_metadata_behavior = :apply_to_host_groups
6766

6867
# This allows you to limit a spec run to individual examples or groups
6968
# you care about by tagging them with `:focus` metadata. When nothing
@@ -116,22 +115,20 @@
116115
config.before(:each) do
117116
Money.default_currency = Money::Currency.new("USD")
118117
end
119-
end
120-
121-
def reset_i18n
122-
I18n.backend = I18n::Backend::Simple.new
123-
end
124118

125-
RSpec.shared_context "with infinite precision", :default_infinite_precision_true do
126-
before do
119+
config.before(:each, :default_infinite_precision_true) do
127120
Money.default_infinite_precision = true
128121
end
129122

130-
after do
123+
config.after(:each, :default_infinite_precision_true) do
131124
Money.default_infinite_precision = false
132125
end
133126
end
134127

128+
def reset_i18n
129+
I18n.backend = I18n::Backend::Simple.new
130+
end
131+
135132
class Money
136133
module Warning
137134
def warn(message); end

0 commit comments

Comments
 (0)