class Typhoeus::Expectation

end
)
:body => SERIALIZERS.serialize(body_obj)
},
‘Content-Type’ => format
:headers => {
Typhoeus::Response.new(
body_obj = { ‘things’ => [ { ‘id’ => ‘foo’ } ] }
format = accept.split(“,”).first
accept = (request.options||{})[‘Accept’] || “application/json”
Typhoeus.stub(“www.example.com”) do |request|
@example Stub a request and get a lazily-constructed response in the format requested.
end
)
end.join(“,”)
ids << widget.id
:body => actual_widgets.inject([]) do |ids, widget|
Typhoeus::Response.new(
actual_widgets = Widget.all
Typhoeus.stub(“www.example.com/widgets”) do
@example Stub a request and get a lazily-constructed response containing data from actual widgets that exist in the system when the stubbed request is made.
#=> true
expected == actual
actual = Typhoeus.get(“www.example.com”)
Typhoeus.stub(“www.example.com”).and_return(expected)
expected = Typhoeus::Response.new
@example Stub a request and get specified response.
responses are returned one by one.
whether they match. If that’s the case, the attached
to the request url and options in order to evaluate
a url and options, like a request. They are compared
of the stubbing mechanism. An expectation contains
This class represents an expectation. It is part

def all

Returns:
  • (Array) - The expectations.

Other tags:
    Example: Return expectations. -
def all
  @expectations ||= []
end

def and_return(response=nil, &block)

Returns:
  • (void) -

Other tags:
    Example: Add response. -
def and_return(response=nil, &block)
  responses << (response.nil? ? block : response)
end

def clear

Other tags:
    Example: Clear expectations. -
def clear
  all.clear
end

def find_by(request)

Other tags:
    Api: - private
def find_by(request)
  all.find do |expectation|
    expectation.matches?(request)
  end
end

def initialize(base_url, options = {})

Other tags:
    Api: - private

Returns:
  • (Expectation) - The created expectation.

Other tags:
    Example: Create expectation. -
def initialize(base_url, options = {})
  @base_url = base_url
  @options = options
  @response_counter = 0
  @from = nil
end

def matches?(request)

Other tags:
    Api: - private

Returns:
  • (Boolean) - True when matches, else false.

Parameters:
  • request (Request) -- The request to check.

Other tags:
    Example: Check if request matches. -
def matches?(request)
  url_match?(request.base_url) && options_match?(request)
end

def options_match?(request)

I checks options and original options.
Check whether the options matches the request options.
def options_match?(request)
  (options ? options.all?{ |k,v| request.original_options[k] == v || request.options[k] == v } : true)
end

def response(request)

Other tags:
    Api: - private

Returns:
  • (Response) - The response.

Other tags:
    Example: Return response. -
def response(request)
  response = responses.fetch(@response_counter, responses.last)
  if response.respond_to?(:call)
    response = response.call(request)
  end
  @response_counter += 1
  response.mock = @from || true
  response
end

def response_for(request)

Other tags:
    Api: - private

Returns:
  • (Typhoeus::Response) - The stubbed response from a

Other tags:
    Example: Find response -
def response_for(request)
  expectation = find_by(request)
  return nil if expectation.nil?
  expectation.response(request)
end

def responses

Other tags:
    Api: - private

Returns:
  • (Array) - The responses.

Other tags:
    Example: Return responses. -
def responses
  @responses ||= []
end

def stubbed_from(value)

Other tags:
    Api: - private

Returns:
  • (Expectation) - Returns self.

Parameters:
  • value (String) -- Value to set.

Other tags:
    Example: Mark expectation. -
def stubbed_from(value)
  @from = value
  self
end

def url_match?(request_url)

all urls.
Nil serves as a placeholder in case you want to match

regexp are checked, nil is always true, else false.
The base_url can be a string, regex or nil. String and
Check whether the base_url matches the request url.
def url_match?(request_url)
  case base_url
  when String
    base_url == request_url
  when Regexp
    !!request_url.match(base_url)
  when nil
    true
  else
    false
  end
end