Skip to content

Commit 50144c4

Browse files
st0012lgebhardt
authored andcommitted
Add database_cleaner to keep integration tests isolated.
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.
1 parent 5d18849 commit 50144c4

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

jsonapi-resources.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
2626
spec.add_development_dependency 'simplecov'
2727
spec.add_development_dependency 'pry'
2828
spec.add_development_dependency 'concurrent-ruby-ext'
29+
spec.add_development_dependency 'database_cleaner'
2930
spec.add_dependency 'activerecord', '>= 4.1'
3031
spec.add_dependency 'railties', '>= 4.1'
3132
spec.add_dependency 'concurrent-ruby'

test/integration/requests/request_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
class RequestTest < ActionDispatch::IntegrationTest
44
def setup
5+
DatabaseCleaner.start
56
JSONAPI.configuration.json_key_format = :underscored_key
67
JSONAPI.configuration.route_format = :underscored_route
78
Api::V2::BookResource.paginator :offset
89
$test_user = Person.find(1001)
910
end
1011

12+
def teardown
13+
DatabaseCleaner.clean
14+
end
15+
1116
def after_teardown
1217
JSONAPI.configuration.route_format = :underscored_route
1318
end

test/test_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'simplecov'
2+
require 'database_cleaner'
23

34
# To run tests with coverage:
45
# COVERAGE=true bundle exec rake test
@@ -443,6 +444,8 @@ class CatResource < JSONAPI::Resource
443444
jsonapi_resources :people
444445
end
445446

447+
DatabaseCleaner.strategy = :transaction
448+
446449
# Ensure backward compatibility with Minitest 4
447450
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
448451

0 commit comments

Comments
 (0)