1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
require 'logger'
require 'rspec'
require 'action_mailer'
require 'active_record'
require 'delayed_job'
require 'delayed/backend/shared_spec'
if ENV['DEBUG_LOGS']
Delayed::Worker.logger = Logger.new(STDOUT)
else
require 'tempfile'
tf = Tempfile.new('dj.log')
Delayed::Worker.logger = Logger.new(tf.path)
tf.unlink
end
ENV['RAILS_ENV'] = 'test'
# Trigger AR to initialize
ActiveRecord::Base # rubocop:disable Void
module Rails
def self.root
'.'
end
end
Delayed::Worker.backend = :test
if ActiveSupport::VERSION::MAJOR < 7
require 'active_support/dependencies'
# Add this directory so the ActiveSupport autoloading works
ActiveSupport::Dependencies.autoload_paths << File.dirname(__FILE__)
else
# Rails 7 dropped classic dependency auto-loading. This does a basic
# zeitwerk setup to test against zeitwerk directly as the Rails zeitwerk
# setup is intertwined in the application boot process.
require 'zeitwerk'
loader = Zeitwerk::Loader.new
loader.push_dir File.dirname(__FILE__)
loader.setup
end
# Used to test interactions between DJ and an ORM
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
ActiveRecord::Base.logger = Delayed::Worker.logger
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :stories, :primary_key => :story_id, :force => true do |table|
table.string :text
table.boolean :scoped, :default => true
end
end
class Story < ActiveRecord::Base
self.primary_key = 'story_id'
def tell
text
end
def whatever(n, _)
tell * n
end
default_scope { where(:scoped => true) }
handle_asynchronously :whatever
end
RSpec.configure do |config|
config.after(:each) do
Delayed::Worker.reset
end
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
|