【发布时间】:2015-09-17 20:24:17
【问题描述】:
我正在寻找在测试外部网页时使用 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`
我也尝试过存根 ourdomain.com:443,但似乎没有什么不同。
【问题讨论】:
标签: rspec capybara integration-testing