我有一个带有链接的页面,该链接打开一个带有简单输入字段的bootstrap模式。我很难理解为什么Capybara不会等待模式打开,并立即失败。
我添加了expect(page).to have_content('Did you')
,因此水豚将等待几秒钟,直到显示模态。
但出于某种原因,它没有等待并提升Capybara::ExpectationNotMet: expected to find text "Did you" in ...."
如果我进入1秒的睡眠状态,它会找到模态,但它不够好,因为我的 spec_helper.rb 中的数据库干净回调被称为:< / p>
config.after(:each) do
DatabaseCleaner.clean
end
这是规范:
RSpec.describe "follower button", type: :request do
it "sends email #15B to owner", :js do
using_wait_time 20 do
FactoryGirl.create(:apartment_with_event)
visit apartment_path(Apartment.last)
click_on 'follow-events'
expect(page).to have_content('Did you')
within('#follow-events-modal') do
fill_in 'follower-email-signup-mail', with: 'follower@example.com'
click_button 'follower-signup-submit'
end
expect(page).to have_content(I18n.t("followers.create.title_success"))
expect(Follower.all.count).to eq(1)
end
end
end
我还在 spec_helper.rb 中设置Capybara.default_max_wait_time = 10
,即使此示例应等待20秒。
忘记提及,我使用 Capybara-wekbit 作为驱动程序。
我真的花了好几个小时试图发现它为什么会发生,而其他例子运行得很好。
更新 添加完整的故障回溯。
Failures:
1) leeron's button sends email #15B to owner
Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
ActionController::RoutingError:
No route matches [GET] "/images/slider/missing.png"
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/rollbar-1.2.13/lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/railties-4.2.4/lib/rails/rack/logger.rb:38:in `call_app'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/railties-4.2.4/lib/rails/rack/logger.rb:20:in `block in call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/railties-4.2.4/lib/rails/rack/logger.rb:20:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/request_store-1.2.0/lib/request_store/middleware.rb:8:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/railties-4.2.4/lib/rails/engine.rb:518:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/railties-4.2.4/lib/rails/application.rb:165:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/rack-1.6.4/lib/rack/urlmap.rb:66:in `block in call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `each'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/capybara-2.7.1/lib/capybara/server.rb:43:in `call'
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
# ------------------
# --- Caused by: ---
# Capybara::ExpectationNotMet:
# expected to find text "Did you" in "...(REMOVED BY ME TO SAVE SPACE)..."
# /Users/etaiso/.rbenv/versions/2.2.3/gemsets/dorbel/gems/capybara-2.7.1/lib/capybara/node/matchers.rb:527:in `block in assert_text'
Finished in 3.87 seconds (files took 9.79 seconds to load)
1 example, 1 failure
答案 0 :(得分:2)
从回调中我们可以看到实际的异常是来自应用程序的路由错误。虽然Capybara正在重试/等待查找器和期望它检查服务器抛出的错误,如果Capybara.raise_server_errors
为真,它将在测试线程中提升它们,以便它们对测试可见。从测试线程中的app线程重新引发异常的副作用是测试线程中的任何当前异常(例如ExpectationNotMet)通常由Capybara处理和重试,由ruby设置为嵌套{ {1}}应用例外。它不是真正的原因,但我们不能因为Capybara重新引发它出现的服务器错误的方式。您可以设置cause
或更好,修复丢失的图像。