我不明白如何设置 webmock(或任何存根库)以仅存根对特定 URL(www.example.com)的特定请求。
我正在浏览器上进行 Cucumber 测试,并且我想允许连接到任何站点,除了我想要存根的那些请求。
对于我的特殊情况,我想存根访问www.example.com/article/:article_id
并交付我之前下载的页面的 HTML 文件。
按照此链接和其他问题
# Gemfile
gem 'webmock'
# features/support/webmock.rb
require 'webmock/cucumber'
我的黄瓜环境文件
# features/env.rb
# Allow all server connections by default
WebMock.allow_net_connect!
Before('@stub-example.com') do
stub_request(:get, 'https://www.example.com').to_rack(FakeExampleDotCom)
end
我目前被困在这个阶段,因为存根不工作,我的测试试图连接到真实的网站。我想WebMock.allow_net_connect!
禁用任何存根。我不能只是disable_net_connect!
因为我想授权每个网站(而不仅仅是本地主机)并且只授权“黑名单”www.example.com
并将其存根。我怎样才能做到这一点 ?
仅供参考:提供 HTML 文件的我的 Sinatra 应用程序
class FakeExampleDotCom < Sinatra::Base
get 'article/:article_id' do
html_response 200, "#{params[:article_id]}_article.html"
end
private
# Returns the HTML file corresponding to the article
def html_response(response_code, file_name)
content_type :html
status response_code
File.open(Rails.root.join('features', 'assets', file_name)).read
end
end