Class: HttpDecoy::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/http_decoy/rspec.rb,
lib/http_decoy/minitest.rb,
lib/http_decoy/definition.rb

Overview

Wraps a named RouteMap so it can be shared across multiple test files and reused across test frameworks.

Framework-specific helper methods are added by whichever integration file is loaded: require "http_decoy/rspec" adds #rspec_helpers, require "http_decoy/minitest" adds #minitest_helpers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, route_map) ⇒ Definition

Returns a new instance of Definition.



13
14
15
16
# File 'lib/http_decoy/definition.rb', line 13

def initialize(name, route_map)
  @name      = name
  @route_map = route_map
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/http_decoy/definition.rb', line 11

def name
  @name
end

#route_mapObject (readonly)

Returns the value of attribute route_map.



11
12
13
# File 'lib/http_decoy/definition.rb', line 11

def route_map
  @route_map
end

Instance Method Details

#minitest_helpersObject

Returns an anonymous module. Include it in a Minitest::Test subclass to register the server lifecycle for every test in that class.

include FakeStripe.minitest_helpers


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/http_decoy/minitest.rb', line 165

def minitest_helpers
  definition = self

  Module.new do
    include HttpDecoy::Minitest

    # See the matching comment in rspec.rb — the explicit `extend` is
    # required because Ruby's `included` hook does not cascade through
    # nested includes.
    define_singleton_method(:included) do |base|
      super(base)
      base.extend(HttpDecoy::Minitest::ClassMethods)
      base.include(HttpDecoy::Minitest::Assertions)
      base._http_decoy_register(definition.name, definition.route_map)
    end

    define_method(:_http_decoy_definition) { definition }
  end
end

#rspec_helpersObject

Returns an anonymous module. Include it in RSpec.configure to register the server lifecycle for every example group in the suite.

RSpec.configure { |c| c.include FakeStripe.rspec_helpers }


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/http_decoy/rspec.rb', line 172

def rspec_helpers
  definition = self

  Module.new do
    include HttpDecoy::RSpec

    # define_singleton_method closes over `definition` from the outer scope.
    # `def self.included` would NOT — def never captures outer locals.
    #
    # `extend` is required here: `include HttpDecoy::RSpec` above only ran
    # HttpDecoy::RSpec.included against *this anonymous module*, not against
    # `base` — Ruby's included hook does not cascade through nested includes.
    # Without this line, `RSpec.configure { |c| c.include Foo.rspec_helpers }`
    # used on its own raises NoMethodError on `_http_decoy_register`.
    define_singleton_method(:included) do |base|
      super(base)
      base.extend(HttpDecoy::RSpec::ClassMethods)
      base._http_decoy_register(definition.name, definition.route_map)
    end

    define_method(:_http_decoy_definition) { definition }
  end
end