简体   繁体   English

如何使用 webmock 伪造对 Capybara/poltergeist 的响应?

[英]How can I fake a response to Capybara/poltergeist using webmock?

I'm testing a webscraper and I'd like to use Webmock to deliver fake websites for faster testing.我正在测试一个网络爬虫,我想使用 Webmock 来提供假网站以进行更快的测试。 When I mock a website, Ruby's native HTTP library works fine, but Capybara doesn't seem capable of receiving the mocked response.当我模拟一个网站时,Ruby 的原生 HTTP 库工作正常,但 Capybara 似乎无法接收模拟响应。 I know that webmock is stubbing low level HTTP requests, and I assume it matters which one capybara uses and which one webmock is configured to use.我知道 webmock 正在存根低级别 HTTP 请求,我认为水豚使用哪一个以及配置使用哪个 webmock 很重要。 However, I need to know how Capybara makes HTTP requests and how I can configure webmock to stub that particular method set.但是,我需要知道 Capybara 如何发出 HTTP 请求,以及如何配置 webmock 以存根该特定方法集。

require 'capybara/poltergeist'
require 'webmock'
require 'pry'
include WebMock::API
WebMock.disable_net_connect!(allow_localhost:true)


Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app, js_errors: false)
end
# Configure Capybara to use Poltergeist as the driver
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist

U = /google.com/

b = Capybara.current_session

stub_request(:any, U).
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
to_return(status:200, body:"abc", headers:{})

puts Net::HTTP.get(U,'/') #=> This returns "abc"

b.visit U
puts b.html  #=> Throws error

The error I'm getting is as follows: command': Request failed to reach server, check DNS and/or server status (Capybara::Poltergeist::StatusFailError)我得到的错误如下: command': Request failed to reach server, check DNS and/or server status (Capybara::Poltergeist::StatusFailError)

I've tried using FakeWeb as well, but that simply was not capable of registering URIs.我也尝试过使用 FakeWeb,但这根本无法注册 URI。 I'm open to using other APIs besides webmock if you think this is the wrong tool for the job.如果您认为这是不适合这项工作的工具,我愿意使用除 webmock 之外的其他 API。

Thanks in advance:)提前致谢:)

Tom Walpole is correct. Tom Walpole是对的。 You can use WebMock to mock things your server is connecting to, but the browser makes its own connections and is unaffected by the changes you make to the server. 您可以使用WebMock模拟服务器连接的内容,但浏览器会建立自己的连接,并且不受您对服务器所做更改的影响。

If you want to fake responses that the browser requests from other servers try something like Puffing Billy . 如果你想假冒浏览器从其他服务器请求的响应尝试像Puffing Billy这样的东西。 Take a look at the Caching capability which can be setup to re-play results (much like VCR). 看看缓存功能,可以设置重新播放结果(很像VCR)。

If you're working with something VERY simple you could try just loading the data you need with Capybara.string . 如果您正在处理非常简单的事情,您可以尝试使用Capybara.string加载所需的数据。 But that's probably too limited for what you want. 但这可能对你想要的东西太有限了。

Capybara doesn't make web requests, it tells the browser where to visit and the browser in turn makes the request. Capybara不会发出Web请求,它会告诉浏览器访问的位置以及浏览器依次发出请求。 The way to do what you want is to use a proxy that can redirect specific browser requests to your own app 执行所需操作的方法是使用可以将特定浏览器请求重定向到您自己的应用程序的代理

There is a newer and better way of doing this.有一种更新更好的方法可以做到这一点。

# spec/spec_helper.rb

RSpec.configure do |config|
  config.before(:each) do |example|
    if example.metadata[:type] == :feature
      Capybara::Webmock.start
    end
  end

  config.after(:suite) do
    Capybara::Webmock.stop
  end
end

Then use the capybara_webmock JavaScript driver:然后使用 capybara_webmock JavaScript 驱动:

# Use Chrome Driver
Capybara.javascript_driver = :capybara_webmock_chrome

https://github.com/hashrocket/capybara-webmock https://github.com/hashrocket/capybara-webmock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM