I'm writing controller test with rspec and after action completed, my job supposed to send email to the admin user. But I'd like to disable this job for my tests or mock it somehow. How can I do this?
I'm using delayed_job_active_record
+ daemons
gems.
class AdminNotificationJob < ActiveJob::Base
queue_as :default
def perform(method, parameter)
User.admin.includes(:profile).each do |admin|
AdminMailer.send(method, admin, parameter).deliver_later
end
end
end
By default, every Rails application has three environments: development, test, and production. Each environment's configuration can be modified similarly. In this case, we can modify our test environment by changing the options found in config/environments/test.rb. Your tests are run under RAILS_ENV=test.
Parallel testing allows you to parallelize your test suite. While forking processes is the default method, threading is supported as well. Running tests in parallel reduces the time it takes your entire test suite to run. The default parallelization method is to fork processes using Ruby's DRb system.
ERB allows you to embed Ruby code within templates. The YAML fixture format is pre-processed with ERB when Rails loads fixtures. This allows you to use Ruby to help you generate some sample data. For example, the following code generates a thousand users: Rails automatically loads all fixtures from the test/fixtures directory by default.
Rails applications generated from JRuby or TruffleRuby will automatically include the with: :threads option. The number of workers passed to parallelize determines the number of threads the tests will use.
#config/environment/test.rb
config.active_job.queue_adapter = :test
# in spec
expect { your_action_that_triggers_job_enqueuing }
.to have_enqueued_job(AdminNotificationJob)
.with(your_arguments)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With