Sha256: 348025f133f72cc043e8061efd0437f054a10cdd174c9072989a047badd364c5

Contents?: true

Size: 1.74 KB

Versions: 7

Compression:

Stored size: 1.74 KB

Contents

require 'gh'
require 'webmock/rspec'
require 'yaml'
require 'fileutils'

RUBY_ENGINE = 'ruby' unless defined? RUBY_ENGINE

module GH
  module TestHelpers
    def backend(layer = subject)
      return layer if layer.backend.nil? or layer.is_a? MockBackend
      backend layer.backend
    end

    def requests
      backend.requests
    end

    def data
      backend.data
    end

    def should_request(num = 1, &block)
      was = requests.count
      yield
      (requests.count - was).should be == num
    end

    def should_not_request(&block)
      should_request(0, &block)
    end

  end

  class MockBackend < Wrapper
    attr_accessor :data, :requests

    def setup(*)
      @data, @requests = {}, []
      super
    end

    def fetch_resource(key)
      key = path_for(key)
      key_fn = sanitize_filename(key)
      file = File.expand_path("../payloads/#{key_fn}.yml", __FILE__)
      @requests << key
       result = @data[key] ||= begin
        unless File.exist? file
          res = allow_http { super }
          FileUtils.mkdir_p File.dirname(file)
          File.write file, [res.headers, res.body].to_yaml
        end

        headers, body = YAML.load_file(file)
        Response.new(body, headers, frontend.full_url(key))
      end

      result = Response.new(result) unless result.is_a? Response
      result
    end

    def sanitize_filename(name)
      name.gsub(/[\?=&]/,"_")
    end

    def reset
      super
      @data.clear
      @requests.clear
    end

    private

    def allow_http
      WebMock.allow_net_connect!
      yield
    ensure
      WebMock.disable_net_connect!
    end
  end
end

RSpec.configure do |c|
  c.include GH::TestHelpers
  c.before { GH::DefaultStack.replace GH::Remote, GH::MockBackend }
  c.after { GH.reset }
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gh-0.13.0 spec/spec_helper.rb
gh-0.12.4 spec/spec_helper.rb
gh-0.12.3 spec/spec_helper.rb
gh-0.12.2 spec/spec_helper.rb
gh-0.12.1 spec/spec_helper.rb
gh-0.12.0 spec/spec_helper.rb
gh-0.11.3 spec/spec_helper.rb