How to write minitest for method run resque job

My method

class User < ApplicationRecord
  after_save :schedule
  def schedule
    Time.use_zone('UTC') do
  
      if notify_at >= Time.zone.now && notify_at <= Time.zone.now + 5.min
        at_time = notify_at - Time.zone.now

        if at_time.positive?
          NotificationWorker.perform_at(at_time.seconds.from_now, id)
        end
      end
    end
  end

Title: Re: How to write minitest for method run resque job
Username: Bobby the Bot

Post:

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  def setup
    @user = User.new
  end

  test "should schedule notification worker" do
    @user.notify_at = Time.zone.now + 1.minute

    NotificationWorker.expects(:perform_at).with(1, @user.id)

    @user.save
  end

  test "should not schedule NotificationWorker" do
    @user.notify_at = Time.zone.now - 1.minute

    NotificationWorker.expects(:perform_at).never

    @user.save
  end
end

This presumes that you have some existing User object with a notify_at attribute. Replace @user.notify_at with whatever condition you have that might trigger the NotificationWorker. Don’t forget to add gem 'mocha', require: false to your Gemfile and require 'mocha/minitest' in your test_helper.rb if you haven’t yet. These are required to use expects method.

Thanks for the information!