1

所以我已经设置了我的 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 时,一切正常。

有任何想法吗?

4

1 回答 1

0

哇。发布问题后,我几乎立即发现了问题。不涉及任何交易。

这是后端和 webkit/selenium 前端之间的竞争问题。使用 Webkit,测试在控制器有机会更新数据库之前执行 @user.reload 和期望语句。对于 Selenium,情况正好相反。

诀窍是让 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
于 2015-02-14T09:56:02.783 回答