Ruby on rails Net::HTTP.new(url.host,url.port)的存根结果
我有一个调用Net::HTTP.new(google_url.host,google_url.port)的函数,我正在尝试找出如何存根测试结果。基本上,我不想每次运行测试时都使用谷歌URL缩短器Ruby on rails Net::HTTP.new(url.host,url.port)的存根结果,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个调用Net::HTTP.new(google_url.host,google_url.port)的函数,我正在尝试找出如何存根测试结果。基本上,我不想每次运行测试时都使用谷歌URL缩短器 def shorten_url(long_url) google_url = URI.parse("https://www.googleapis.com/urlshortener/v1/url") data = JSON.generate({"longUrl" => "#{lon
def shorten_url(long_url)
google_url = URI.parse("https://www.googleapis.com/urlshortener/v1/url")
data = JSON.generate({"longUrl" => "#{long_url}"})
header = {"Content-Type" => "application/json"}
http = Net::HTTP.new(google_url.host, google_url.port)
http.use_ssl = true
short_url = ""
res = http.request_post(google_url.path, data, header)
jsonResponse = JSON.parse(res.body)
short_url = jsonResponse["id"]
end
基本上,我希望能够设置该函数的结果
我曾经尝试过这样的方法:Net::HTTP.any_instance.stubs(:HTTP.new).returns(“www.test.com”)
,但我不知道如何让它工作
class HTTP < Protocol
....
# Creates a new Net::HTTP object.
# If +proxy_addr+ is given, creates an Net::HTTP object with proxy support.
# This method does not open the TCP connection.
def HTTP.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil)
h = Proxy(p_addr, p_port, p_user, p_pass).newobj(address, port)
h.instance_eval {
@newimpl = ::Net::HTTP.version_1_2?
}
h
end
....
end
class-HTTP
签出-这听起来像是它将完全满足您的需求。由于应用程序的性质,我需要在不安装任何gems的情况下完成此操作。谢谢你。