openSUSE / open-build-service

Build and distribute Linux packages from sources in an automatic, consistent and reproducible way #obs
https://openbuildservice.org
GNU General Public License v2.0
936 stars 440 forks source link

Use database_cleaner for functional tests #1231

Open ChrisBr opened 9 years ago

ChrisBr commented 9 years ago

When we use_js in a test, the database is in an inconsistent state! This caused us yesterday some headaches...

the tests are being wrapped in database transactions, so any code running outside the actual test process (like, say, a server process servicing a Selenium-driven browser request) does not see the database fixture I’ve so carefully assembled.

http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

We already have the database_cleaner gem in our Gemfile, unfortunately we don't really use it. When I change our testhelper to sth like this:

self.use_transactional_fixtures = false

setup do
      DatabaseCleaner.strategy = :deletion
      DatabaseCleaner.start
end

teardown do
      DatabaseCleaner.clean
end

the behavior of the tests is like expected.

But changing this would slow down our testsuite. We should only use this for our Capybara / functional tests. We would need for instance 'minitest-metadata' gem to annotate our tests like it 'does sth', js: true to setup database_cleaner in our setup function accordingly.

This is just as a reminder, we don't need that now but I wanted to document this for the future.

coolo commented 9 years ago

you better merge webui and api for real and then you don't need this crap and can use transactions

ChrisBr commented 9 years ago

Not really, because ActiveRecord creates a new connection to the database for each thread. That becomes a problem when you use Capybara because it setups it's own connection and won't see the data you create. So this isn't related to webui <-> api but to Capybara js tests.