Module: Operations::TestHelpers

Defined in:
lib/operations/test_helpers.rb

Overview

This module contains helpers for simplifaction of testing of operation-related concerns.

Examples:

require "operations/test_helpers"

module RailsHelper
  config.include Operations::TestHelpers
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.empty_contractObject



13
14
15
# File 'lib/operations/test_helpers.rb', line 13

def self.empty_contract
  @empty_contract ||= OperationContract.build { schema { nil } }
end

Instance Method Details

#policy_errors(policy, contract = Operations::TestHelpers.empty_contract, **context) ⇒ Object

Works exactly the same way as #precondition_errors but for policies.



63
64
65
66
67
68
69
70
# File 'lib/operations/test_helpers.rb', line 63

def policy_errors(policy, contract = Operations::TestHelpers.empty_contract, **context)
  component = Operations::Components::Policies.new(
    [policy],
    message_resolver: contract.message_resolver
  )
  result = component.call({}, context)
  result.errors.to_h
end

#precondition_errors(precondition, contract = Operations::TestHelpers.empty_contract, **context) ⇒ Object

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.

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.

Examples:

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


50
51
52
53
54
55
56
57
# File 'lib/operations/test_helpers.rb', line 50

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