MiniTest、Capybara、Fixture,Database_Cleaner 第二次未通过

MiniTest, Capybara, Fixture, Database_Cleaner not passing at second time

以下测试第一次通过(在 rake db:test:prepare 之后)但 失败 在随后的 运行.

中给出了错误

Capybara::ElementNotFound: Unable to find css "#sale_payment_btn"

require "test_helper"

DatabaseCleaner.strategy = :transaction

class SaleFeatureTest < Capybara::Rails::TestCase
  include Warden::Test::Helpers
  Warden.test_mode!

  self.use_transactional_fixtures = false

  setup do
    login_as users(:admin), scope: :user
    Capybara.current_driver = :selenium #:webkit
    DatabaseCleaner.start
  end

  teardown do
    DatabaseCleaner.clean
    Warden.test_reset!
  end

  test "Sale" do        
    visit(new_sale_path) # create new sale and redirect to it
    assert page.has_css?('#sale_payment_btn') # gave error at second time
    find(:css, '#sale_payment_btn').click # this create payment
  end

因为我将 selenium 与 chrome 一起使用,所以我可以看到销售的 ID。我注意到后续测试的 ID 相同。即 980190963

我的理论是

  1. database_cleaner 未按预期运行。 (虽然我在 test.log 文件上看到数据库清理 sql 命令,但我看到数据保留在数据库中)

  2. visit 不会创建新的 @sale(如 here 所述,尽管我使用的是 minitest)因为 #sal_payment_btn 未呈现(销售已经有payment 第一个 运行)。

我已经纠结了半天了。我试过了

第二运行我还是考不上。 运行手动测试没问题

我哪里做错了什么?

P.S。我正在使用以下 gems

minitest-rails-capybara
selenium-webdriver
chromedriver-helper
database_cleaner
minitest-around
pg

我已阅读以下内容

我已确认问题出在我的 database_cleaner 设置上。

简短版

:transaction 策略不适用于我的设置 cucumber+webkit/selenium。更改为 :deletion 策略。

我学到了什么

看来我学习还不够。我在寻找答案的过程中发现了以下内容。

  1. 有人问Why is the database cleaner transaction strategy not working with Cucumber and Selenium? - 没有回答

  2. How to set up database_cleaner for Rails with Cucumber and RSpec表示

    The :transaction strategy does not work with Selenium

  3. database_cleaner readme 表示 :transaction 策略强加了更多的工作

    However, if you wind up needing to use multiple database connections in your tests (i.e. your tests run in a different process than your application) then using this strategy becomes a bit more difficult

其他值得一读