Job Code and Testing Strategies

Learn about job code and the job testing strategies in Ruby on Rails.

We'll cover the following

For all the reasons we don’t want business logic in our controllers, we don’t want business logic in our jobs. Likewise, for all the reasons we want to convert the raw data types being passed into richly typed objects in our controllers, we want to do that in our jobs, too.

We passed in a widget ID to our job, which means our job should locate the widget. After that, it should defer to another class that implements the business logic. Because this is still widget creation and the job is called PostWidgetCreationJob, we’ll create a new method on WidgetCreator called post_widget_creation and have the job trigger that.

Let’s write the job code and then fill in the new method. Because we aren’t using Active Job, we can’t use bin/rails g job. We also can’t use ApplicationJob in its current form, but it is nice to have a base class for all jobs. Let’s replace the Rails-provided ApplicationJob with one that is specific to Sidekiq.

Get hands-on with 1400+ tech skills courses.