Skip to content

Commit 71fa0fc

Browse files
authored
feat: Add error for using with_options in define_enum_for matcher (#1693)
We're adding an explicit error message when users mistakenly use with_options instead of with_values in the define_enum_for matcher. This will help clarify the correct usage and prevent confusion.
1 parent fa4c9d5 commit 71fa0fc

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/shoulda/matchers/active_record/define_enum_for_matcher.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ def with_values(expected_enum_values)
320320
self
321321
end
322322

323+
def with_options(**)
324+
raise NotImplementedError,
325+
'with_options is not a valid qualifier for the define_enum_for matcher. '\
326+
'Did you mean to use with_values instead?'
327+
end
328+
323329
def with_prefix(expected_prefix = true)
324330
options[:prefix] = expected_prefix
325331
self

spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ def self.statuses
103103
end
104104
end
105105

106+
describe 'when with_options is used instead of with_values' do
107+
it 'raises an appropriate error' do
108+
record = build_record_with_array_values(
109+
model_name: 'Example',
110+
attribute_name: :attr,
111+
)
112+
113+
assertion = lambda do
114+
expect(record).
115+
to define_enum_for(:attr).
116+
with_options(active: 0, archived: 1)
117+
end
118+
119+
message = 'with_options is not a valid qualifier for the define_enum_for matcher. '\
120+
'Did you mean to use with_values instead?'
121+
122+
expect(&assertion).to raise_error(NotImplementedError, message)
123+
end
124+
end
125+
106126
describe 'with both attribute name and enum values specified' do
107127
context 'when the actual enum values are an array' do
108128
context 'if the attribute is not defined as an enum' do

0 commit comments

Comments
 (0)