Question

This code works fine without WebMock.

Raising an exception:

Paperclip::AdapterRegistry::NoHandlerError:
   No handler found for #<URI::HTTP:0x007ff3852cefb8 URL:http://www.example.com/images/foo.jpg>
# ./spec/support/api_mock.rb:34:in `process_image_for'

Test:

let( :image_url  ) { 'http://www.example.com/images/foo.jpg'  }
...
stub_request(:post, image_url)
  .to_return(:status => 200, :body => File.read('spec/fixtures/image.jpg'), :headers => {})
...hit Sinatra app...

api_mock.rb:

def self.process_image_for suggestion, params
  if params[:image]
    suggestion.image = URI.parse( params[:image] )  # line 34
    suggestion.save!
  end
end
Was it helpful?

Solution

It works. FWIW, both File.read and File.open work:

stub_request(:post, image_url)
  .to_return(
     :status => 200,
     :body => File.read('spec/fixtures/image.jpg'),
     :headers => {}
  )

Just remember to require 'webmock/rspec' at the top of the test.

OTHER TIPS

it is required to use headers: {"Content-Type" => 'image/jpg'} or any valid content type expected by Paperclip

eg.

stub_request(:get, "http://img.youtube.com/vi/123123123123/0.jpg")
  .to_return(
    status: 200,
    body:  File.read('spec/fixtures/images/sample.jpg'),
    headers: {"Content-Type" => 'image/jpg'}
  )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top