【发布时间】:2017-12-29 04:57:27
【问题描述】:
我正在使用 Capybara 测试一个基本表单 - 当用户填写并提交时,它应该创建一个新的 User 记录
it "creates a new user" do
visit some_random_path
# Fill In Form
fill_in("name", with: "foo bar")
fill_in("email", with: "foo.bar@example.com")
expect do
click_button("Submit")
end.to change { User.count }.by(1)
end
expect 块会引发错误,因为它没有看到 User.count 增加 1。但是我注意到,如果我执行类似的操作
click_button("Submit") && sleep(0.1)
效果很好!
所以看起来 RSpec 正在尝试快速检查 - 即在 Capybara 运行的浏览器有机会实际提交表单并将结果提交到数据库之前。
我没有使用任何 JavaScript,只是一个普通的旧 :webkit 规范。
对为什么会发生这种情况有什么想法吗?
下面是我的 Capybara 配置。我有一个多租户应用程序(我使用apartment gem 来处理重要的事情)所以我使用localhost 和lvh.me 作为应用程序主机,如下所述,但我无法想象这会干扰以上。
Capybara.configure do |config|
config.ignore_hidden_elements = true
Capybara.default_driver = :webkit
config.javascript_driver = :webkit
end
Capybara::Webkit.configure do |config|
config.block_unknown_urls
config.allow_url("lvh.me")
end
RSpec.configure do |config|
config.before(:suite) do
Capybara.always_include_port = true
# The default is to treat each spec as single tennat, in which case
# we want to hit localhost. Hitting the Capbyara default of www.example.com
# causes the apartment setup to try and parse the `www` as a subdomain
Capybara.app_host = "http://localhost"
end
config.before(:each, multi_tenant: true) do
# For multi-tenant specs, use "lvh.me"
Capybara.app_host = "http://lvh.me"
end
end
谢谢!
【问题讨论】:
标签: rspec capybara capybara-webkit