Skip to content

Instantly share code, notes, and snippets.

@krzykamil
Created October 7, 2022 09:04
Show Gist options
  • Save krzykamil/ea9509b162352fa9cd6509a8da55b897 to your computer and use it in GitHub Desktop.
Save krzykamil/ea9509b162352fa9cd6509a8da55b897 to your computer and use it in GitHub Desktop.
rspec matcher for decorator
# spec/support/matchers/decorations.rb
module Support
module Matchers
module Decorations
extend RSpec::Matchers::DSL
def decorated_object_attributes(model, klass: nil, user: nil, context: nil)
context ||= {}
attrs = { object: model }
attrs[:class] = klass if klass
context[:user] = user if user
attrs[:context] = a_hash_including(context) if context.any?
an_object_having_attributes(**attrs)
end
matcher :be_decorated do |model|
match(notify_expectation_failures: true) do |decorated|
expect(decorated).to match(
decorated_object_attributes(model, klass: @class, user: @user, context: @context)
)
end
chain :with_class do |klass|
@class = klass
end
chain :with_context do |context|
@context = context
end
chain :with_user do |user|
@user = user
end
end
RSpec.configure do |rspec|
rspec.include self, type: :operation
rspec.include self, type: :decorator
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment