After upgrading my Jets application from version 3 to 5, I encountered some flaky specs. When running the entire spec suite, numerous errors are raised, with most of them being ActiveRecord::AssociationTypeMismatch errors. However, when I isolate and run one of these failing specs, it works perfectly fine.
Upon analysis, it seems that the failed specs might be related to a database problem.
Here’s how my spec_helper.rb file looks:
ENV['JETS_TEST'] = '1'
ENV['JETS_ENV'] ||= 'test'
require 'simplecov'
SimpleCov.start do
add_group 'Models', 'app/models'
add_group 'Controllers', 'app/controllers'
add_group 'Jobs', 'app/jobs'
add_group 'Services', 'app/services'
add_group 'lib', 'lib'
changed_files = `git diff --name-only origin/master`.split("\n")
add_group 'Changed' do |source_file|
changed_files.detect do |filename|
source_file.filename.ends_with?(filename)
end
end
add_filter '/spec/'
add_filter '/config/'
add_filter 'lib/helper.rb'
end
# Ensure AWS API is never called. Fixture home folder does not contain ~/.aws/credentials
ENV['HOME'] = 'spec/fixtures/home'
require 'byebug'
require 'fileutils'
require 'jets'
abort('The Jets environment is running in production mode!') if Jets.env == 'production'
Jets.boot
require 'jets/spec_helpers'
require 'vcr'
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
module Helpers
def payload(name)
JSON.parse(File.read("spec/fixtures/payloads/#{name}.json"))
end
end
VCR.configure do |config|
config.allow_http_connections_when_no_cassette = false
config.default_cassette_options = { record: :none, erb: true, match_requests_on: [:uri] }
config.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
# config.debug_logger = $stderr
config.hook_into :webmock
config.ignore_localhost = true
config.configure_rspec_metadata!
end
RSpec::Matchers.define_negated_matcher :not_change, :change
RSpec.configure do |config|
config.example_status_persistence_file_path = 'spec/failures.txt'
config.include Helpers
config.include FactoryBot::Syntax::Methods
config.before do
FactoryBot.find_definitions if FactoryBot.factories.count == 0
end
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
FactoryBot.find_definitions
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.before :all, rotate_seeding: true do
Faker::Config.random = Random.new(rand(3))
end
config.after :all, perform_enqueued: true do
Faker::Config.random = nil
end
end
By the way, I noticed that if I remove the line require ‘jets/spec_helpers’ , the specs run successfully, except for the Controller Specs.