![](/img/trans.png)
[英]rails, rspec, capybara, webkit/selenium, devise, logged out after every request
[英]Rails/RSpec/Capybara - transactionless database cleaning works with Selenium but not Webkit
因此,我已经将RSpec环境设置为对RSpec Capybara测试使用截断清除策略,但是当我使用Webkit作为Javascript驱动程序时,我仍然发现某些东西仍将我的测试包装在事务中。
我没有Selenium的这个问题,这让我感到困惑。
这是与webkit相关的RSpec配置:
Capybara.javascript_driver = :webkit
Capybara.register_driver :webkit do |app|
Capybara::Webkit::Driver.new(app).tap do |driver|
driver.allow_url "fonts.googleapis.com"
driver.allow_url "dl.dropboxusercontent.com"
end
end
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
DatabaseCleaner.clean_with :transaction
end
config.after(:each) do
ActionMailer::Base.deliveries.clear
end
config.around(:each, type: :feature, js: true) do |ex|
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
self.use_transactional_fixtures = false
ex.run
self.use_transactional_fixtures = true
DatabaseCleaner.clean
end
我的功能测试如下所示:
feature "profile", js: true do
describe "a confirmed user with a valid profile" do
before(:each) do
@user = FactoryGirl.create :user
signin(@user.email, @user.password)
end
scenario 'can edit name' do
visit edit_user_profile_path
fill_in :user_name, with: 'New name'
click_button :Submit
@user.reload
expect(@user.name).to eq('New name')
expect(current_path).to eq show_user_path
end
end
end
如果我使用Webkit运行该测试,它将失败,但是使用Selenium,则它可以通过。
我已经尝试了一些调试。 如果在#update动作中放入调试器语句,则会看到它正确更新了数据库。 如果那时我连接到测试数据库,则可以在数据库中看到新信息,这意味着此更新不能包含在事务中。 但是,但是在.spec @user的调试器中,仍然可以看到FFaker在factory_girl中生成的原始名称。 这使我相信测试是在事务内进行的。
当我将JavaScript驱动程序更改为Selenium时,一切正常。
有任何想法吗?
哇。 发布问题后,我几乎立即发现了问题。 不涉及任何交易。
这是后端与Webkit / Selenium前端之间的竞赛问题。 使用Webkit,测试将在控制器有机会更新数据库之前执行@ user.reload和Expect语句。 与硒相反。
诀窍是让Capybara等待页面重新加载。 我将测试更改为此:
scenario 'can edit name' do
visit edit_user_profile_path
fill_in :user_name, with: 'New name'
click_button :Submit
expect(current_path).to eq show_user_path
@user.reload
expect(@user.name).to eq('New name')
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.