我正在寻找用JavaScript测试外部网页的存根请求。 Puffing-billy看起来是要走的路 - 这是代码:
describe 'Covetable click behavior' do
let(:user) { create(:user) }
describe 'clicking button', :type => :feature, js: true do
it 'makes a request to our api' do
visit 'http://externaldomain.com'
allow_any_instance_of(Analytics::EventsController).to receive(:create)
sleep(7) # may be race conditions at work; helps following test pass
expect execute_script(typeof eventAnalytics == 'function').to eq true
# testing that the javascript making the request is loaded
proxy.stub('https://ourdomain/analytics/event').and_return(redirect_to: 'http://localhost:3000/analytics/event?name=$likeButtonClicked')
click_button 'Like'
expect_any_instance_of(Analytics::EventsController).to receive(:create)
end
end
end
Capybara.javascript_driver = :selenium_billy
config.around(:each, type: :feature) do |example|
WebMock.allow_net_connect!
VCR.turned_off { example.run }
end
感谢任何前进存根请求的建议!
编辑:更新了代码,并添加了更深入的日志记录。
代理存根不直接引发错误,但不拦截&重定向正在进行的http请求。 test.log文件要么完全跳过请求,要么允许它:puffing-billy: PROXY GET succeeded for 'https://ourdomain.com:443/analytics/event...'
Failure/Error: Unable to find matching line from backtrace
Exactly one instance should have received the following message(s) but didn't: create`
我也尝试过截断mydomain.com:443,但似乎没有什么区别。
答案 0 :(得分:0)
Capybara控制浏览器,然后浏览器发出请求,运行javascript等等。因此,你不能使用stub_request在测试中存根响应,因为浏览器对此没有任何了解,它只是向服务器应用程序发出请求。您可以使用puffing billy来重定向来自浏览器的请求,但是您需要确保在驱动程序中正确设置了代理。您可以在注册自己的驱动程序实例时手动执行此操作,也可以使用其中一个pilly billy提供的驱动程序设置(请参阅plening billy自述文件:selenium_billy等)