Class: StubRequests::DSL

Inherits:
Object show all
Defined in:
lib/stub_requests/dsl.rb,
lib/stub_requests/dsl/define_method.rb,
lib/stub_requests/dsl/method_definition.rb

Overview

Module DSL takes the id of a registered service

Examples:

Register service with endpoints

StubRequests.register_service(:documents, "https://company.com/api/v1") do
  register_endpoints do
    get "documents/:id", as: :documents_show
    get "documents",     as: :documents_index
    post "documents",    as: :documents_create
  end
end

Create a receiver module for the stub methods

module Stubs; end

Stubs.instance_methods #=> []

Define the endpoint methods using the DSL

StubRequests::DSL.define_stubs(
  :documents, receiver: Stubs
)

# This turns the module Stubs into the following:

Stubs.instance_methods #=> [:stub_documents_show, :stub_documents_index, :stub_documents_create]
module Stubs
  def stub_documents_show(id:, &block)
     stub_endpoint(:documents_show, id: id, &block)
  end

  def stub_documents_index(&block)
     stub_endpoint(:documents_index, &block)
  end

  def stub_documents_create(&block)
     stub_endpoint(:documents_create, &block)
  end
 end

Use the helper methods in your tests

include Stubs

let(:document_id)   { 1234 }
let(:request_body)  { { key: "value" }.to_json }
let(:response_body) { { id: document_id, key: "value" }.to_json }

before do
  stub_documents_create do
    with(body: request_body)
    to_return(body: response_body)
  end

  stub_documents_show(id: document_id) do
    with(body: request_body)
    to_return(body: response_body)
  end
end

it "stubs the requests nicely" do
  create_uri = URI("https://company.com/api/v1/documents")
  response   = Net::HTTP.post(create_uri)
  expect(response).to be_json_eql(response_body.to_json)

  show_uri = URI("https://company.com/api/v1/documents/#{document_id}")
  response = Net::HTTP.post(create_uri)
  expect(response).to be_json_eql(response_body.to_json)
end

Author:

Since:

  • 0.1.4

Defined Under Namespace

Classes: DefineMethod, MethodDefinition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_id, receiver: nil) ⇒ DSL

Initialize a new instance of DSL

Parameters:

  • service_id (Symbol)

    the id of a registered service

  • receiver (Module)

    the receiver of the stub methods

Since:

  • 0.1.4



92
93
94
95
# File 'lib/stub_requests/dsl.rb', line 92

def initialize(service_id, receiver: nil)
  @endpoints = StubRequests::EndpointRegistry[service_id]
  @receiver  = receiver
end

Instance Attribute Details

#endpointsObject (readonly)

Since:

  • 0.1.4



84
85
86
# File 'lib/stub_requests/dsl.rb', line 84

def endpoints
  @endpoints
end

#receiverObject (readonly)

Since:

  • 0.1.4



80
81
82
# File 'lib/stub_requests/dsl.rb', line 80

def receiver
  @receiver
end

#serviceObject (readonly)

Since:

  • 0.1.4



76
77
78
# File 'lib/stub_requests/dsl.rb', line 76

def service
  @service
end

Instance Method Details

#define_stubsvoid

This method returns an undefined value.

Defines stub methods for #endpoints in the #receiver

Since:

  • 0.1.4



103
104
105
106
107
108
109
# File 'lib/stub_requests/dsl.rb', line 103

def define_stubs
  receiver.send(:include, StubRequests::API)

  method_definitions.each do |method_definition|
    DefineMethod.new(method_definition, receiver).define
  end
end

#method_definitionsObject

Since:

  • 0.1.4



123
124
125
126
127
# File 'lib/stub_requests/dsl.rb', line 123

def method_definitions
  @method_definitions ||= endpoints.map do |endpoint|
    MethodDefinition.new(endpoint.id, endpoint.route_params)
  end
end

This method returns an undefined value.

Prints stub methods for #endpoints to STDOUT

Since:

  • 0.1.4



117
118
119
120
121
# File 'lib/stub_requests/dsl.rb', line 117

def print_stubs
  method_definitions.each do |method_definition|
    puts("#{method_definition}\n\n")
  end
end