class Typhoeus::Expectation

This class represents an expectation. It is part of the stubbing mechanism. An expectation contains a url and options, like a request. They are compared to the request url and options in order to evaluate whether they match. If that’s the case, the attached responses are returned one by one.

@example Stub a request and get specified response.

expected = Typhoeus::Response.new
Typhoeus.stub("www.example.com").and_return(expected)

actual = Typhoeus.get("www.example.com")
expected == actual
#=> true

@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.

Typhoeus.stub("www.example.com/widgets") do
  actual_widgets = Widget.all
  Typhoeus::Response.new(
    :body => actual_widgets.inject([]) do |ids, widget|
      ids << widget.id
    end.join(",")
  )
end

@example Stub a request and get a lazily-constructed response in the format requested.

Typhoeus.stub("www.example.com") do |request|
  accept = (request.options[:headers]||{})['Accept'] || "application/json"
  format = accept.split(",").first
  body_obj = { 'things' => [ { 'id' => 'foo' } ] }

  Typhoeus::Response.new(
    :headers => {
      'Content-Type' => format
    },
    :body => SERIALIZERS[format].serialize(body_obj)
  )
end