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

五迷三道 提交于 2019-12-21 21:43:54

问题


The following test passed first time (after rake db:test:prepare) but failed gave error on subsequent run.

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

As I used selenium with chrome, I could see the ID of the sale. I noticed that the ID was the same for subsequent test. i.e. 980190963

My theory is that

  1. database_cleaner not functioning as expected. (Although I see DB cleaning sql commands on test.log file, I saw data remained in database)

  2. visit does not create new @sale (as stated here though I use minitest) because #sal_payment_btn is not rendered (sale already has payment on first run).

I'm pulling my hair around for half day now. I have tried

  • webkit driver
  • different cleaning strategy truncation, deletion

and I still can't pass the test on second run. It is running okay on manual test.

What and where did I do wrong?

P.S. I'm using the following gems

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

I have read the following

  • http://blog.berylliumwork.com/2013/07/rails-4-devise-3-minitest-and-capybara.html
  • Database Cleaner not working in minitest rails
  • Rails minitest, database cleaner how to turn use_transactional_fixtures = false

回答1:


I've confirmed that the problem was my setup of database_cleaner.

Short version

:transaction strategy was not working for my setup cucumber+webkit/selenium. Changed to :deletion strategy.

What I learnt

It seemed I didn't study enough. I found out the following during my search for answer.

  1. Someone asked Why is the database cleaner transaction strategy not working with Cucumber and Selenium? - no answer

  2. How to set up database_cleaner for Rails with Cucumber and RSpec stated that

    The :transaction strategy does not work with Selenium

  3. database_cleaner readme stated that :transaction strategy imposes bit more working

    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

Other worthy reading

  • Why you should be using Database Cleaner's deletion strategy
  • most recommended way to set up database_cleaner with rails, rspec, capybara and selenium - http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
  • Rails 4, Devise 3, MiniTest and Capybara for user login


来源:https://stackoverflow.com/questions/30955261/minitest-capybara-fixture-database-cleaner-not-passing-at-second-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!