我可以覆盖水豚的页面.body值吗



我遇到了一个问题,即存根响应不起作用(使用带有远程浏览器的docker(,但需要针对外部请求进行测试

有没有办法通过Capybara对回复进行硬编码?

# This is what we would normally do
WebMock.stub_request(:get, 'http://example.com').to_return(status: 200, body: IO.read('spec/factories/submission_stubs/mystub.html'))
# In theory this should return mystub.html, but that is not working in my case.
# But since we're using a remote webdriver via docker it has no idea about my 'mock'
visit 'http://example.com'
# So I'm looking to simply override page.body to be `mystub.html`
puts page.body

我还试图从这个答案中直接读取文件:水豚在我的电脑中打开一个html文件

但似乎该文件没有正确安装在我的远程驱动程序系统上

编辑:我一辈子都无法像预期的那样完成这项工作。响应总是返回一个空的HTML页面。我的解决方案是简单地将我的存根响应加载到云中并将其公开,这样我的远程浏览器就可以访问它

您所指的解决方案是正确的。这里的诀窍是我们加载路径,直到文件所在的目录。不包括文件名本身

下面是一个工作示例:

context 'testing' do
before do
# the trick here we load the path up till the directory where the file exist
# don't include the file name itself
Capybara.app = Rack::File.new('spec/factories/submission_stubs')
end
it 'loads the file' do
visit 'mystub.html'
puts page.body
end
end

您不能使用WebMock截取浏览器从测试中看到的响应,因为它只截取本地请求。要存根/模拟对浏览器请求的响应,您应该查看一个可编程代理,如https://github.com/oesmith/puffing-billy

最新更新