Skip to content

Commit

Permalink
test(has_result_short_syntax): add :[] specs
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Oct 29, 2022
1 parent 9531420 commit b5c57a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ module Concern

class_methods do
##
# NOTE: Delegates to `result` instead of aliasing in order to have an ability
# to use the same RSpec stubs for short and usual syntax.
# @internal
# NOTE: Delegates to `result` instead of aliasing in order to have an ability
# to use the same RSpec stubs for short and usual syntax.
#
# For example:
# For example:
#
# allow(Service).to receive(:result).with(foo: :bar)and_call_original
# allow(Service).to receive(:result).with(foo: :bar)and_call_original
#
# works for both `Service.result(foo: :bar)` and `Service[foo: :bar]`.
# works for both `Service.result(foo: :bar)` and `Service[foo: :bar]`.
#
def [](**kwargs)
result(**kwargs)
def [](...)
result(...)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

RSpec.describe ConvenientService::Service::Plugins::HasResultShortSyntax::Concern::ClassMethods do
example_group "instance methods" do
describe "#[]" do
include ConvenientService::RSpec::Matchers::DelegateTo

let(:service_class) do
Class.new.tap do |klass|
klass.class_exec(described_class) do |mod|
include ConvenientService::Common::Plugins::HasConstructor::Concern
include ConvenientService::Service::Plugins::HasResult::Concern

extend mod

def result(*args, **kwargs, &block)
:result_value
end
end
end
end

let(:args) { [:foo] }
let(:kwargs) { {foo: :bar} }
let(:block) { proc { :foo } }

specify do
expect { service_class[*args, **kwargs, &block] }
.to delegate_to(service_class, :result)
.with_arguments(*args, **kwargs, &block)
.and_return_its_value
end
end
end
end

0 comments on commit b5c57a7

Please sign in to comment.