Class: Faraday::Adapter::Test::Stubs

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/adapter/test.rb

Overview

A stack of Stubs

Defined Under Namespace

Classes: NotFound

Instance Method Summary collapse

Constructor Details

#initialize(strict_mode: false) {|_self| ... } ⇒ Stubs

Returns a new instance of Stubs.

Yields:

  • (_self)

Yield Parameters:



70
71
72
73
74
75
76
77
# File 'lib/faraday/adapter/test.rb', line 70

def initialize(strict_mode: false)
  # { get: [Stub, Stub] }
  @stack = {}
  @consumed = {}
  @strict_mode = strict_mode
  @stubs_mutex = Monitor.new
  yield(self) if block_given?
end

Instance Method Details

#delete(path, headers = {}, &block) ⇒ Object



122
123
124
# File 'lib/faraday/adapter/test.rb', line 122

def delete(path, headers = {}, &block)
  new_stub(:delete, path, headers, &block)
end

#empty?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/faraday/adapter/test.rb', line 79

def empty?
  @stack.empty?
end

#get(path, headers = {}, &block) ⇒ Object



102
103
104
# File 'lib/faraday/adapter/test.rb', line 102

def get(path, headers = {}, &block)
  new_stub(:get, path, headers, &block)
end

#head(path, headers = {}, &block) ⇒ Object



106
107
108
# File 'lib/faraday/adapter/test.rb', line 106

def head(path, headers = {}, &block)
  new_stub(:head, path, headers, &block)
end

#match(env) ⇒ Object

Parameters:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/faraday/adapter/test.rb', line 84

def match(env)
  request_method = env[:method]
  return false unless @stack.key?(request_method)

  stack = @stack[request_method]
  consumed = (@consumed[request_method] ||= [])

  @stubs_mutex.synchronize do
    stub, meta = matches?(stack, env)
    if stub
      removed = stack.delete(stub)
      consumed << removed unless removed.nil?
      return stub, meta
    end
  end
  matches?(consumed, env)
end

#options(path, headers = {}, &block) ⇒ Object



126
127
128
# File 'lib/faraday/adapter/test.rb', line 126

def options(path, headers = {}, &block)
  new_stub(:options, path, headers, &block)
end

#patch(path, body = nil, headers = {}, &block) ⇒ Object



118
119
120
# File 'lib/faraday/adapter/test.rb', line 118

def patch(path, body = nil, headers = {}, &block)
  new_stub(:patch, path, headers, body, &block)
end

#post(path, body = nil, headers = {}, &block) ⇒ Object



110
111
112
# File 'lib/faraday/adapter/test.rb', line 110

def post(path, body = nil, headers = {}, &block)
  new_stub(:post, path, headers, body, &block)
end

#put(path, body = nil, headers = {}, &block) ⇒ Object



114
115
116
# File 'lib/faraday/adapter/test.rb', line 114

def put(path, body = nil, headers = {}, &block)
  new_stub(:put, path, headers, body, &block)
end

#strict_mode=(value) ⇒ Object

Set strict_mode. If the value is true, this adapter tries to find matched requests strictly, which means that all of a path, parameters, and headers must be the same as an actual request.



147
148
149
150
151
152
153
154
# File 'lib/faraday/adapter/test.rb', line 147

def strict_mode=(value)
  @strict_mode = value
  @stack.each_value do |stubs|
    stubs.each do |stub|
      stub.strict_mode = value
    end
  end
end

#verify_stubbed_callsObject

Raises an error if any of the stubbed calls have not been made.



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/faraday/adapter/test.rb', line 131

def verify_stubbed_calls
  failed_stubs = []
  @stack.each do |method, stubs|
    next if stubs.empty?

    failed_stubs.concat(
      stubs.map do |stub|
        "Expected #{method} #{stub}."
      end
    )
  end
  raise failed_stubs.join(' ') unless failed_stubs.empty?
end