问题
I'm writing tests for my rails application. I want to see only '.' and '*' symbols, when rspec running. But I'm watching HTTP logs and don't know how disable them. Piece of rspec output from my terminal:
.............get http://0.0.0.0:3000/device_info/?api_session=%23%7Bapi_session%7D
User-Agent: "Faraday v0.8.7"
404
content-type: "text/html"
x-cascade: "pass"
connection: "close"
server: "thin 1.5.1 codename Straight Razor"
.....get http://0.0.0.0:3000/device_info/12345?api_session=%23%7Bapi_session%7D
User-Agent: "Faraday v0.8.7"
400
..
I assume that the reason may be configuration of 'faraday' gem. How can I disable this logs from my rspec output?
回答1:
faraday does not log anything per default:
irb(main):001:0> require "faraday"
=> true
irb(main):002:0> Faraday.get 'http://sushi.com/nigiri/sake.json'
=> #<Faraday::Response:0x007ff48f0422a8 @env={:method=>:get, :body=>"", :url=>#<URI::HTTP:0x007ff48f03bd40 URL:http://sushi.com/nigiri/sake.json>, :request_headers=>{"User-Agent"=>"Faraday v0.8.7"}, :parallel_manager=>nil, :request=>{:proxy=>nil}, :ssl=>{}, :status=>302, :response_headers=>{"date"=>"Thu, 25 Jul 2013 14:36:42 GMT", "server"=>"Apache/2.2.22 (Ubuntu)", "x-powered-by"=>"PHP/5.3.10-1ubuntu3.6", "location"=>"http://sushi.com/?f", "vary"=>"Accept-Encoding", "content-length"=>"20", "connection"=>"close", "content-type"=>"text/html", "set-cookie"=>"WEBUK=WUK08; path=/"}, :response=>#<Faraday::Response:0x007ff48f0422a8 ...>}, @on_complete_callbacks=[]>
i assume that you have some configuration block in your app like this:
conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
faraday.request :url_encoded
faraday.response :logger
faraday.adapter Faraday.default_adapter
end
conn.get '/nigiri/sake.json'
if you remove the logger
line, than there should not be any output to STDOUT.
回答2:
Just remove the response :logger
config when testing (!Rails.env.test?
)
Faraday.new(url: endpoint) do |faraday|
faraday.request :url_encoded
faraday.response :logger if !Rails.env.test?
faraday.adapter Faraday.default_adapter
end
回答3:
Sometimes it is not possible to modify the Faraday instance configuration because it is defined within a gem. For example the 'oauth2' gem uses Faraday and on error the output is to console which during test outputs within the .
sequence.
However, the standard Faraday configuration uses Logger
from the standard library.
So an alternative is to mock the add
method that is used by the Logger instance methods info
, debug
etc.:
allow_any_instance_of(Logger).to receive(:add)
I'd recommend not doing that globally, and only if faraday.response :logger if !Rails.env.test?
is not possible. So just in the specific specs that are generating the unwanted output.
来源:https://stackoverflow.com/questions/17857542/how-disable-http-logs-from-rspec-output