我在我的应用程序中使用数据表,记录通过ajax加载到表中。现在我正在为数据表的页面编写测试,并且必须等到记录加载。我配置了 capybara webkit 并在测试中放入 js: true 。但是当我运行测试时我收到此错误
Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
ActionController::RoutingError:
No route matches [GET] "/images/sort_both.png"
在我的浏览器中,我检查了控制台,但没有收到此错误,所有图像均已找到。
这是我的 webkit 配置
Capybara.javascript_driver = :webkit
Capybara::Webkit.configure do |config|
config.raise_javascript_errors = false
end
我正在使用 wait_for_ajax 方法
def wait_for_ajax
Timeout.timeout(Capybara.default_max_wait_time) do
loop do
active = page.evaluate_script('jQuery.active')
break if active == 0
end
end
end
这是我的测试
scenario 'check pending contributions', js: true do
login_as analyst, scope: :analyst
visit '/analyst/contributions'
wait_for_ajax
expect(page).to have_content "#{contribution.user_name}"
end
现在我想修复这个路由错误,或者直接忽略它,因为我只想测试逻辑,而不是 js 错误。
最佳答案
该错误不是 JS 错误,而是请求资源时服务器引发的错误。您可以通过设置 Capybara.raise_server_errors = false 来告诉您的测试忽略错误,但是更好的解决方案是修复您的 Assets 管道,以便所有 Assets 都能在测试环境中正确提供服务。可能的是,您之前已经在测试环境中预编译了 Assets ,并且遗留的 list 文件阻止了它重新编译新添加的 Assets 。您可以通过运行类似
来解决这个问题RAILS_ENV=test rake assets:clobber
此外,假设通过 ajax 调用加载 #{contribution.user_name}
文本,则不需要 wait_for_ajax
方法调用,因为 have_content
已经等待/重试。
关于ruby-on-rails - Capybara webkit 忽略 ActionController::RoutingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45193622/