Skip to content

Commit

Permalink
Add database_cleaner to keep integration tests isolated.
Browse files Browse the repository at this point in the history
If we create/modify/delete records (via requests) in integration tests,
it might affect other tests' test result. The ideal way is to keep
integration test data isolated. And by adding database_cleaner, we can
make sure database is reset between each test cases.
  • Loading branch information
st0012 authored and lgebhardt committed Mar 22, 2019
1 parent 5d18849 commit 50144c4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions jsonapi-resources.gemspec
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'concurrent-ruby-ext'
spec.add_development_dependency 'database_cleaner'
spec.add_dependency 'activerecord', '>= 4.1'
spec.add_dependency 'railties', '>= 4.1'
spec.add_dependency 'concurrent-ruby'
Expand Down
5 changes: 5 additions & 0 deletions test/integration/requests/request_test.rb
Expand Up @@ -2,12 +2,17 @@

class RequestTest < ActionDispatch::IntegrationTest
def setup
DatabaseCleaner.start
JSONAPI.configuration.json_key_format = :underscored_key
JSONAPI.configuration.route_format = :underscored_route
Api::V2::BookResource.paginator :offset
$test_user = Person.find(1001)
end

def teardown
DatabaseCleaner.clean
end

def after_teardown
JSONAPI.configuration.route_format = :underscored_route
end
Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.rb
@@ -1,4 +1,5 @@
require 'simplecov'
require 'database_cleaner'

# To run tests with coverage:
# COVERAGE=true bundle exec rake test
Expand Down Expand Up @@ -443,6 +444,8 @@ class CatResource < JSONAPI::Resource
jsonapi_resources :people
end

DatabaseCleaner.strategy = :transaction

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

Expand Down

0 comments on commit 50144c4

Please sign in to comment.