1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Encapsulate spec helper methods into a module

It's a good idea to encapsulate methods rather than have them hanging
around in global scope.
This commit is contained in:
Sandro Turriate 2010-01-29 15:20:15 -05:00
parent 2bf184a853
commit e6e5990497
3 changed files with 33 additions and 24 deletions

View file

@ -1,20 +1,6 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
describe HTTParty::Request do
def stub_response(body, code = 200)
unless @http
@http = Net::HTTP.new('localhost', 80)
@request.stub!(:http).and_return(@http)
@request.stub!(:uri).and_return(URI.parse("http://foo.com/foobar"))
end
response = Net::HTTPResponse::CODE_TO_OBJ[code.to_s].new("1.1", code, body)
response.stub!(:body).and_return(body)
@http.stub!(:request).and_return(response)
response
end
before do
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', :format => :xml)
end

View file

@ -10,15 +10,8 @@ def file_fixture(filename)
open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename.to_s}")).read
end
def stub_http_response_with(filename)
format = filename.split('.').last.intern
data = file_fixture(filename)
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
response = Net::HTTPOK.new("1.1", 200, "Content for you")
response.stub!(:body).and_return(data)
http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => format)
http_request.stub!(:perform_actual_request).and_return(response)
HTTParty::Request.should_receive(:new).and_return(http_request)
Spec::Runner.configure do |config|
config.include HTTParty::StubResponse
end

View file

@ -0,0 +1,30 @@
module HTTParty
module StubResponse
def stub_http_response_with(filename)
format = filename.split('.').last.intern
data = file_fixture(filename)
response = Net::HTTPOK.new("1.1", 200, "Content for you")
response.stub!(:body).and_return(data)
http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => format)
http_request.stub!(:perform_actual_request).and_return(response)
HTTParty::Request.should_receive(:new).and_return(http_request)
end
def stub_response(body, code = 200)
unless @http
@http = Net::HTTP.new('localhost', 80)
@request.stub!(:http).and_return(@http)
@request.stub!(:uri).and_return(URI.parse("http://foo.com/foobar"))
end
response = Net::HTTPResponse::CODE_TO_OBJ[code.to_s].new("1.1", code, body)
response.stub!(:body).and_return(body)
@http.stub!(:request).and_return(response)
response
end
end
end