gpt4 book ai didi

ruby - 为什么使用 AJAX 的 rspec 示例无法正常工作?

转载 作者:数据小太阳 更新时间:2023-10-29 08:12:02 26 4
gpt4 key购买 nike

我在运行包含 ajax 的集成测试时遇到问题。

例如下面的测试

scenario 'displays next page when scrolling to the bottom of the page', js: true do
scroll_to_bottom_of_page
wait_for_ajax

expect(all('.follow-up').count).to eq(30)
end

页面方法scroll_to_bottom的实现

def scroll_to_bottom_of_page
page.execute_script "$('.off-canvas-wrap').scrollTo('.exit-off-canvas');"
end

wait_for_ajax 方法的实现

module WaitForAjax
def wait_for_ajax
Timeout.timeout(Capybara.default_wait_time) do
loop until finished_all_ajax_requests?
end
end

def finished_all_ajax_requests?
page.evaluate_script('jQuery.active').zero?
end
end

RSpec.configure do |config|
config.include WaitForAjax, type: :feature
end

我基本上想测试我的无限滚动功能是否正常工作。

当我将脚本更改为以下内容时,它工作正常。

scenario 'displays next page when scrolling to the bottom of the page', js: true do
scroll_to_bottom_of_page
sleep 1

expect(all('.follow-up').count).to eq(30)
end

这不是很可靠。如果 ajax 调用超过 1 秒怎么办?我不太喜欢随机添加 sleep 1、2 或 3。

如果您能帮助找出 wait_for_ajax 的问题,我们将不胜感激。

谢谢。

我刚刚创建了一个测试应用程序。请检查一下

https://github.com/ryannealmes/capybara-bug

最佳答案

在 capybara 论坛上发帖后,我发现有些选择器等待异步调用完成,有些则没有。为了让这个规范通过,我所要做的就是将期望值更新到下面

expect(page).to have_css('.follow-up', count: 30)

如果您遇到此问题并想了解更多信息,下面列出了我传递的一些有用资源。

关于ruby - 为什么使用 AJAX 的 rspec 示例无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28632166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com