Created
October 7, 2022 09:04
-
-
Save krzykamil/ea9509b162352fa9cd6509a8da55b897 to your computer and use it in GitHub Desktop.
rspec matcher for decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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