Sha256: 72f61dd823d78030e711780f90b968b46182ef967a55a088132b306f3dd1dd05

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module ApiExampleGroup
  include Rack::Test::Methods

  def self.included(base)
    base.let(:server) { MongoBrowser::Models::Server.current }
    base.extend(ClassMethods)
  end

  module ClassMethods
    # Helper for describing API endpoints
    #
    # Usage:
    #   describe_endpoint :delete, "/databases/:db_name" do
    #     let(:db_name) { "first_database" }
    #     # some assertions here
    #   end
    def describe_endpoint(method, route_path, &block)
      describe "#{method.upcase} #{route_path}" do
        let(:path) do
          path = route_path.clone
          route_path.scan(/(:(\w+))/).each do |place_holder, name|
            path.sub!(place_holder, instance_eval(name).to_s)
          end

          path
        end

        let(:do_request) { send(method.downcase.to_sym, path) }
        subject(:response) { do_request }

        # Create a new context with route description
        by_path = -> route { route.route_path.match(/^#{route_path.split("?").first}/) }
        route = described_class.routes.find(&by_path)

        if route and route.route_description
          describe route.route_description do
            instance_eval(&block)
          end
        else
          instance_eval(&block)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongo_browser-0.2.5 spec/support/api_example_group.rb