← 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: "user@example.com")
  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

Did you like this article? You can follow me on Twitter, subscribe to the newsletter or subscribe to the RSS feed if you'd like to follow along.

Unsubscribe anytime. By submitting your email, you agree to our privacy policy.