Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable ruby on rails job for test

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
like image 671
divideByZero Avatar asked Nov 30 '16 15:11

divideByZero


People also ask

How do I change the test environment of a Rails application?

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.

What is parallel testing in Ruby?

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.

How do I embed Ruby code in a rails template?

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.

What is the use of threads option in Ruby on rails?

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.


1 Answers

#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)
like image 77
Andrey Deineko Avatar answered Oct 13 '22 19:10

Andrey Deineko