Skip to content

Commit

Permalink
optimize supports_feature_mixin helper
Browse files Browse the repository at this point in the history
This ensures unsupported_reason is stubbed for both supports and non-supports cases
(the number of calls to supports is reducing)

Since supports? and #unsupported_reason is just a minor shim over .unsupported_reason,
left those methods as is. Only stubbing .unsupported_reason

This also removes the allow_any_instance_of
  • Loading branch information
kbrock committed Mar 1, 2024
1 parent 098b7a3 commit 4d8dd9e
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions spec/support/supports_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,24 @@ module SupportsHelper
# when testing a model that receives multiple supports,
# put this down first to allow the other supports to work fine.
def stub_supports_all_others(model)
allow_any_instance_of(model).to receive(:supports?).and_call_original
allow(model).to receive(:unsupported_reason).and_call_original
end

# when testing requests, ensure the model supports a certain attribute
def stub_supports(model, feature = :update, supported: true)
def stub_supports(model, feature = :update, supported: true, reason: nil)
model = model.class unless model.kind_of?(Class)
feature = feature.to_sym

receive_supports = receive(:supports?).with(feature).and_return(supported)
allow(model).to receive_supports
allow_any_instance_of(model).to receive_supports
reason = supported ? nil : (reason || SupportsFeatureMixin.default_supports_reason)
allow(model).to receive(:unsupported_reason).with(feature, :instance => anything).and_return(reason)

allow(model).to receive(:types_supporting).with(feature).and_return([nil] + model.descendants.map(&:name))
if supported
allow(model).to receive(:types_supporting).with(feature).and_return([nil] + model.descendants.map(&:name))
end
end

def stub_supports_not(model, feature = :update, reason = nil)
model = model.class unless model.kind_of?(Class)
feature = feature.to_sym

stub_supports(model, feature, :supported => false)

reason ||= SupportsFeatureMixin.default_supports_reason
receive_reason = receive(:unsupported_reason).with(feature).and_return(reason)
allow(model).to(receive_reason)
allow_any_instance_of(model).to(receive_reason)
stub_supports(model, feature, :supported => false, :reason => reason)
end
end
end
Expand Down

0 comments on commit 4d8dd9e

Please sign in to comment.