Sha256: e5633d73c802c70cde1e1ead25b53498d84250e3ec8294cbdd42c3155cc83795

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'sanford-protocol'

require 'sanford/runner'

module Sanford

  class TestRunner
    include Sanford::Runner

    attr_reader :handler, :response

    def init!
      if !@request.kind_of?(Sanford::Protocol::Request)
        @request = test_request(@request)
      end
      @response = build_response catch(:halt){ @handler.init; nil }
    end

    # we override the `run` method because the TestRunner wants to control
    # storing any generated response. If `init` generated a response, we don't
    # want to `run` at all.

    def run
      @response ||= build_response catch_halt{ @handler.run }
    end

    protected

    def test_request(params)
      Sanford::Protocol::Request.new('name', params)
    end

    def build_response(response_args)
      Sanford::Protocol::Response.new(response_args.status, response_args.data) if response_args
    end

    module Helpers
      module_function

      def test_runner(handler_class, params = nil, logger = nil)
        TestRunner.new(handler_class, params || {}, logger)
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sanford-0.8.0 lib/sanford/test_runner.rb