如何在轨道中对HTTP的http请求固执



我有一些这样的代码库,我想使用rspec test favicon_href,但是如您所愿,favicon_href会调用页面函数,我知道我可以模拟页面函数,但是为此,阶段我想嘲笑给定URL的HTTP请求,因此我将WebMock Gem的语法用于存根HTTP请求,但似乎WebMock与Mechanagize不兼容,它总是在下面显示错误知道如何求解它或任何宝石可以在机械化上使用HTTP请求?

代码

  def favicon_href
    @favicon_href ||= 
      begin
        page.at(FAVICON_DOM).attributes['href'].value # finding <link> elements
      rescue Exception
        '/favicon.ico' # there are some situation the favicon's not <link>'
      end
  end
  def page
    @page ||= mechanize.get(url)
  end
  def mechanize
    @mechanize ||= Mechanize.new
  end

错误

Failure/Error: @page ||= mechanize.get(valid_url(url))
 WebMock::NetConnectNotAllowedError:
   Real HTTP connections are disabled. Unregistered request: GET https://tsaohucn.wordpress.com/ with headers {'Accept'=>'*/*', 'Accept-Charset'=>'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Accept-Encoding'=>'gzip,deflate,identity', 'Accept-Language'=>'en-us,en;q=0.5', 'Connection'=>'keep-alive', 'Host'=>'tsaohucn.wordpress.com', 'Keep-Alive'=>'300', 'User-Agent'=>'Mechanize/2.7.5 Ruby/2.3.1p112 (http://github.com/sparklemotion/mechanize/)'}
   You can stub this request with the following snippet:
   stub_request(:get, "https://tsaohucn.wordpress.com/").
     with(headers: {'Accept'=>'*/*', 'Accept-Charset'=>'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Accept-Encoding'=>'gzip,deflate,identity', 'Accept-Language'=>'en-us,en;q=0.5', 'Connection'=>'keep-alive', 'Host'=>'tsaohucn.wordpress.com', 'Keep-Alive'=>'300', 'User-Agent'=>'Mechanize/2.7.5 Ruby/2.3.1p112 (http://github.com/sparklemotion/mechanize/)'}).
     to_return(status: 200, body: "", headers: {})
   registered request stubs:
   stub_request(:get, "https://tsaohucn.wordpress.com/").
     with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'})
   stub_request(:any, "http://api.stripe.com/")
   stub_request(:any, "/api.stripe.com/")
   ============================================================

存在Webmock和Net-Http-Persistent之间的不兼容。

请参阅https://github.com/bblimke/webmock#connecting-on-nethtpstart

添加

WebMock.allow_net_connect!(:net_http_connect_on_start => true)

到您的测试设置。

相关内容

  • 没有找到相关文章

最新更新