Sha256: 3c10bc5b6bea7b613d52d3ca0ab1223f4d281703b57672ed790121e959af4a0a

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'spec_helper'
require 'ostruct'

class MyService < ResponseState::Service
  response_states :success, :failure
  attr_reader :object, :pass

  def initialize(object, pass)
    @object = object
    @pass = pass
  end

  def call
    yield (success_response or failure_response)
  end

  private

  def success_response
    return unless pass
    send_state :success, 'Yay! It works.', object
  end

  def failure_response
    send_state :failure, 'Boo! It failed.'
  end
end

describe 'Using the service' do
  let(:object) { Object.new }
  let(:yielded) { OpenStruct.new(message: nil, context: nil) }
  before do
    MyService.(object, pass) do |response|
      response.success { yielded.message = response.message; yielded.context = response.context }
      response.failure { yielded.message = response.message; yielded.context = response.context }
    end
  end

  context 'when pass is true' do
    let(:pass) { true }

    it 'has a happy message' do
      expect(yielded.message).to eq 'Yay! It works.'
    end

    it 'has a valid context' do
      expect(yielded.context).to eq object
    end
  end

  context 'when pass is false' do
    let(:pass) { false }

    it 'has a sad message' do
      expect(yielded.message).to eq 'Boo! It failed.'
    end

    it 'has a nil context' do
      expect(yielded.context).to eq nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
response_state-0.4.0 spec/features/full_example_spec.rb