lib/operations/test_helpers.rb
# Used to test messages generated by preconditions
# If one calls `precondition.call` intests and it returns `Failure`
# there is no way to check is a proper text message will be rendered.
#
# @example
#   subject(:precondition) { described_class::Precondition.new }
#   let(:contract) { described_class::Contract.new }
#
#   describe "#call" do
#     subject(:errors) { precondition_errors(precondition, contract, **context) }
#
#     let(:context) { { entity: entity } }
#
#     context "when entity is pokable" do
#       let(:entity) { build_stubbed(:entity) }
#
#       it { is_expected.to be_empty }
#     end
#
#     context "when entity is not pokable" do
#       let(:entity) { build_stubbed(:entity) }
#
#       specify do
#         expect(errors).to eq({
#           nil => [{ code: :some_failure, text: "Unable to poke entity" }]
#         })
#       end
#     end
#   end
#
# We need to pass contract since preconditions are using the
# contract's message rendering and we want to ensure that the
# translation is placed correctly in the scope of the operation.
Used to test messages generated by preconditions
If one calls `precondition.call` intests and it returns `Failure`
there is no way to check is a proper text message will be rendered.
Used to test messages generated by preconditions If one calls ` precondition . call ` intests and it returns ` Failure ` there is no way to check is a proper text message will be rendered .
def precondition_errors(precondition, contract = Operations::TestHelpers.empty_contract, **context)
    component = Operations::Components::Preconditions.new(
      [precondition],
      message_resolver: contract.message_resolver
    )
    result = component.call({}, context)
    result.errors.to_h
  end
def precondition_errors ( precondition , contract = Operations :: TestHelpers . empty_contract , ** context ) component = Operations :: Components :: Preconditions . new ( [ precondition ] , message_resolver : contract . message_resolver ) result = component . call ( { } , context ) result . errors . to_h end
Used to test messages generated by preconditions
If one calls `precondition.call` intests and it returns `Failure`
there is no way to check is a proper text message will be rendered.
Used to test messages generated by preconditions If one calls ` precondition . call ` intests and it returns ` Failure ` there is no way to check is a proper text message will be rendered .
precondition
<not_specific>
contract
<not_specific>
empty_contract
<not_specific>
context
<not_specific>
example
precondition) { described_class::Precondition.new } let(:contract) { described_class::Contract.new } describe "#call" do subject(:errors) { precondition_errors(precondition, contract, **context) } let(:context) { { entity: entity } } context "when entity is pokable" do let(:entity) { build_stubbed(:entity) } it { is_expected.to be_empty } end context "when entity is not pokable" do let(:entity) { build_stubbed(:entity) } specify do expect(errors).to eq({ nil => [{ code: :some_failure, text: "Unable to poke entity" }] }) end end end We need to pass contract since preconditions are using the contract's message rendering and we want to ensure that the translation is placed correctly in the scope of the operation.
precondition ) { described_class :: Precondition . new } let ( : contract ) { described_class :: Contract . new } describe " #call " do subject ( : errors ) { precondition_errors ( precondition contract ** context ) } let ( : context ) { { entity : entity } } context " when entity is pokable " do let ( : entity ) { build_stubbed ( : entity ) } it { is_expected . to be_empty } end context " when entity is not pokable " do let ( : entity ) { build_stubbed ( : entity ) } specify do expect ( errors ) . to eq ( { nil = > [ { code : : some_failure text : " Unable to poke entity " } ] } ) end end end We need to pass contract since preconditions are using the contract ' s message rendering and we want to ensure that the translation is placed correctly in the scope of the operation .