Database Cleaner not working in minitest rails
Short answer:
gem install "minitest-around"
Long answer:
before/after or setup/teardown in minitest are NOT hooks as in rspec, therefore you can't have multiple before/after or setup/teardown in minitest, since what they do is just redefining the method.
To solve this issue, you can use minitest-around
, which adds support for multiple before
/after
or setup
/teardown
and around
, simply add the gem to your test group:
# put in your Gemfile
gem 'minitest-around', group: :test
For setting up the database_cleaner, you can have it as you want, following is an example of the setup:
# tests/support/database_cleaner.rb
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
class Minitest::Rails::ActionController::TestCase
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
end
And in your test files:
# tests/your/test/file_test.rb
require 'support/database_cleaner'
# assertions here ...
That's it, see the Github for detailed info.
MiniTest, Capybara, Fixture, Database_Cleaner not passing at second time
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.
Someone asked Why is the database cleaner transaction strategy not working with Cucumber and Selenium? - no answer
How to set up database_cleaner for Rails with Cucumber and RSpec stated that
The
:transaction
strategy does not work with Seleniumdatabase_cleaner
readme stated that:transaction
strategy imposes bit more workingHowever, 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
withrails
,rspec
,capybara
andselenium
- 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
database cleanup with capybara
You need to put the page.must_have_content 'USER SETTING & INFO'
before the DatabaseCleaner.clean
line. This is because when you click a button it can return immediately while the request triggered by the button click happens asynchronously. In your case this means you click the button, then clean the database, meanwhile the request to create the new user is sent and most likely arrives after the database has already been cleared thereby creating a user that doesn't get cleared.
Usually the DatabaseCleaner methods would be called from setup/teardown with minitest (before/append_after blocks with RSpec) to ensure that database cleaning doesn't occur until the test is complete, and that they get run even if something in the test fails.
Environment data not found in the schema. with database-cleaner when calling cucumber repeatedly on Rails 6
Turns out this problem was specific to database_cleaner
1.7.
A recent upgrade to 1.8.3 made this error stop happening.
(associated Github issue)
Rails minitest, database cleaner how to turn use_transactional_fixtures = false
You have a few options. One is to create a test without transactional fixtures and hope that the changes you make to the test database isn't going to break any other tests.
class SomethingTest < ActiveSupport::TestCase
self.use_transactional_fixtures = false
def test_something_with_after_commit
# do work here, which will change your test database
end
end
Another option you have is to keep the transactional fixtures, but invoke the after_commit callback manually.
class SomethingTest < ActiveSupport::TestCase
def test_something_with_after_commit
something = Something.new
something.save
something.after_commit
# verify things happened as expected
end
end
And yet another option is to move the logic out of the after_commit
callback into a new object, where you can write proper tests for it without relying on the callbacks to be invoked.
Related Topics
Script to Run Against Stdin If No Arg; Otherwise Input File =Argv[0]
How to Use Mongodb Ruby Driver to Do a "Group" (Group By)
How to Require a Block in Ruby
Ruby - Does Array a Contain All Elements of Array B
How to Disable All Form_For Input Fields in Ruby on Rails App
Pg_Config, Ruby Pg, Postgresql 9.0 Problem After Upgrade, Centos 5
Custom Filtering of Parameters in Rails 3 Using Config.Filter_Parameters
Parsing Date from Text Using Ruby
Ruby Keyword Arguments of Method
Rake Aborted! Stack Level Too Deep
How to Extend Redcarpet to Support Auto Linking User Mentions
Ruby Google_Drive Gem Oauth2 Saving
Suppresing Output to Console with Ruby
Configure Webrick to Use Automatically Generated Self-Signed Ssl/Https Certificate
Ruby: Yield Block from a Block
Accessing Variables from Included Files in Ruby