Sha256: e68646c8616802b8f6d3c503696121f029bed5dba8c0e46783e1f77822d65c8b

Contents?: true

Size: 1.97 KB

Versions: 12

Compression:

Stored size: 1.97 KB

Contents

module TyphoeusMocks

  # This stubs response of GET /resource
  #
  # @param [Hash] options
  # @option options [Array<Hash>] :collection
  # @option options [Integer] :total
  # @option options [String] :url
  # @option options [Integer] :status - HTTP status
  def mock_index(options={})
    req = Typhoeus::Request.any_instance
    response = mock(
      code: options[:status] || 200,
      body: {
        collection: options[:collection] || [],
        total: options[:total] || 0
      }.to_json,
      request: mock(url: options[:url] || "mockedurl"),
      time: 1234
    )
    req.stub(:on_complete).and_yield(response)
  end

  # This stubs response of GET /resource/:id
  #
  # @param [Hash] options
  # @option options [Array<Hash>] :attributes
  # @option options [String] :url
  # @option options [Integer] :status - HTTP status
  def mock_show(options={})
    req = Typhoeus::Request.any_instance
    response = mock(
      code: options[:status] || 200,
      body: options[:attributes].to_json,
      request: mock(url: options[:url] || "mockedurl"),
      time: 1234
    )
    req.stub(:on_complete).and_yield(response)
  end

  # Mocks a Typhoeus::Response
  # @param [Hash] options
  # @option [Integer] code - http response code. default: 200
  # @option [String] body - response body
  # @option [String] url - requested url
  # @return [Typhoeus::Response]
  def mock_response(options={})
    mock_response = Typhoeus::Response.new()
    mock_response.stub(:code).and_return(options[:code]||200)
    mock_response.stub(:headers).and_return('whatever')
    mock_response.stub(:time).and_return(0.1)
    mock_response.stub(:body).and_return options[:body]
    mock_response.stub!(:request).and_return(mock(:url => options[:url] || "mocked-url"))
    mock_response
  end

  # Mocks Typhoeus POST Request and returns a mocked_response
  # @param [Hash] options, see mock_response options.
  def mock_post_with(options={})
    Typhoeus::Request.stub!(:post).and_return(mock_response(options))
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
logical_model-0.3.7 test/typhoeus_mocks.rb
logical_model-0.3.6 test/typhoeus_mocks.rb
logical_model-0.3.5 test/typhoeus_mocks.rb
logical_model-0.3.4 test/typhoeus_mocks.rb
logical_model-0.3.3 test/typhoeus_mocks.rb
logical_model-0.3.2 test/typhoeus_mocks.rb
logical_model-0.3.1 test/typhoeus_mocks.rb
logical_model-0.3.0 test/typhoeus_mocks.rb
logical_model-0.2.22 test/typhoeus_mocks.rb
logical_model-0.2.21 test/typhoeus_mocks.rb
logical_model-0.2.20 test/typhoeus_mocks.rb
logical_model-0.2.19 test/typhoeus_mocks.rb