Skip to content

Commit 58b85a1

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 c6f2ec6 commit 58b85a1

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
@@ -112,22 +111,20 @@
112111
# test failures related to randomization by passing the same `--seed` value
113112
# as the one that triggered the failure.
114113
Kernel.srand config.seed
115-
end
116-
117-
def reset_i18n
118-
I18n.backend = I18n::Backend::Simple.new
119-
end
120114

121-
RSpec.shared_context "with infinite precision", :default_infinite_precision_true do
122-
before do
115+
config.before(:each, :default_infinite_precision_true) do
123116
Money.default_infinite_precision = true
124117
end
125118

126-
after do
119+
config.after(:each, :default_infinite_precision_true) do
127120
Money.default_infinite_precision = false
128121
end
129122
end
130123

124+
def reset_i18n
125+
I18n.backend = I18n::Backend::Simple.new
126+
end
127+
131128
class Money
132129
module Warning
133130
def warn(message); end

0 commit comments

Comments
 (0)