[英]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.