2012-12-19 47 views
2

這段代碼在沒有WebMock的情況下工作正常。如何使用WebMock模擬Sinatra應用程序中的Paperclip調用?

引發異常:

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' 

測試:

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 

回答

8

它的工作原理。 FWIW,無論File.readFile.open工作:

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

只記得require 'webmock/rspec'在測試的頂部。

1

它需要使用headers: {"Content-Type" => 'image/jpg'}或任何有效的內容類型由回形針

如預期。

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'} 
) 
相關問題