如何从Rspec输出中禁用http日志?

我正在为我的rails应用程序编写测试。 我只想看到’。’ 当rspec运行时,和’*’符号。 但我正在观看HTTP日志,不知道如何禁用它们。 我终端输出的一块rspec:


.............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 ..

我假设原因可能是’法拉第’gem的配置。 如何从我的rspec输出中禁用此日志?

法拉第不会默认记录任何内容:

 irb(main):001:0> require "faraday" => true irb(main):002:0> Faraday.get 'http://sushi.com/nigiri/sake.json' => #:get, :body=>"", :url=>#, :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=>#}, @on_complete_callbacks=[]> 

我假设你的应用程序中有一些配置块,如下所示:

 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' 

如果删除logger行,则不应该有任何输出到STDOUT。

只需在测试时删除response :logger config( !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 

有时无法修改法拉第实例配置,因为它是在gem中定义的。 例如,’oauth2’gem使用法拉第,并且在出错时输出是控制台,在测试输出期间. 序列。

但是,标准法拉第配置使用标准库中的Logger

所以另一种方法是模拟Logger实例方法infodebug等使用的add方法:

 allow_any_instance_of(Logger).to receive(:add) 

我建议不要全局,只有faraday.response :logger if !Rails.env.test? 不可能。 所以只是在产生不需要的输出的特定规格中。