2012-12-19 3 views
2

이 코드는 WebMock없이 제대로 작동합니다.Sinatra 앱에서 WebMock을 사용하여 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'} 
)