简体   繁体   English

Rails / RSpec / Capybara-无事务的数据库清理可与Selenium一起使用,但不适用于Webkit

[英]Rails/RSpec/Capybara - transactionless database cleaning works with Selenium but not Webkit

So I've setup my RSpec environment to use a truncation cleaning strategy for my RSpec Capybara tests but I still find that something is still wrapping my test in a transaction when I use Webkit as my Javascript driver. 因此,我已经将RSpec环境设置为对RSpec Capybara测试使用截断清除策略,但是当我使用Webkit作为Javascript驱动程序时,我仍然发现某些东西仍将我的测试包装在事务中。

I don't have this problem with Selenium, which has got me stumped. 我没有Selenium的这个问题,这让我感到困惑。

Here's the relevant RSpec config with webkit: 这是与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

And my feature test looks like this: 我的功能测试如下所示:

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

If I run this test with Webkit it fails, but with Selenium it passes. 如果我使用Webkit运行该测试,它将失败,但是使用Selenium,则它可以通过。

I've experimented with some debugging. 我已经尝试了一些调试。 If I put a debugger statement in the #update action I see that it updates the database correctly. 如果在#update动作中放入调试器语句,则会看到它正确更新了数据库。 If I connect to the test database at that time I can see the new information in the database, which means that this update cannot be wrapped in a transaction. 如果那时我连接到测试数据库,则可以在数据库中看到新信息,这意味着此更新不能包含在事务中。 However, but in the debugger in the .spec @user still see the original name as generated by FFaker in factory_girl. 但是,但是在.spec @user的调试器中,仍然可以看到FFaker在factory_girl中生成的原始名称。 This leads me to believe that the test is ran inside a transaction. 这使我相信测试是在事务内进行的。

When I change my JavaScript driver to Selenium it all works fine. 当我将JavaScript驱动程序更改为Selenium时,一切正常。

Any ideas? 有任何想法吗?

Wow. 哇。 I found the problem almost immediately after posting the question. 发布问题后,我几乎立即发现了问题。 No transactions were involved. 不涉及任何交易。

It was a race issue between the backend and webkit/selenium front end. 这是后端与Webkit / Selenium前端之间的竞赛问题。 With Webkit the test was executing the @user.reload and the expect statements before the controller had a chance to update the database. 使用Webkit,测试将在控制器有机会更新数据库之前执行@ user.reload和Expect语句。 With Selenium it was the other way around. 与硒相反。

The trick is to make Capybara wait for the page reload. 诀窍是让Capybara等待页面重新加载。 I changed my test to this: 我将测试更改为此:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM