← til

Mocking class methods using Minitest

December 21, 2018
ruby

Minitest has a convoluted API for mocking class methods, so in order to do so you'll have to use another gem.

I suggest Mocha.

require "mocha/minitest"

class CronJobProcessor
  def self.process(args)
     # ...
  end
end

class CronEngine
  def process_job
    CronJobProcessor.process(foo: "bar")
    EmailSender.send_notification(to: "[email protected]")
  end
end

describe CronEngine do
  it "does what cron engines do" do
    CronJobProcessor.expects(:process).with(foo: "bar")
    EmailSender.expects(:send_notification)

    CronEngine.new.process_job
  end
end