max pleaner
max pleaner

Reputation: 26788

minitest - expect a method to be called when it was extended from a Module

I am writing a test for the following code:

module Clockwork
  every(10.minutes, SomeBackgroundJob)
end

every is an instance method of the Clockwork::Methods module.

In RSpec I can write the following to test that a module method gets called:

expect_any_instance_of(Clockwork::Methods).to(
  receive(:every).with(10.minutes, SomeBackgroundJob)
)

However I'm not sure how do to this in minitest/mocha.

I am trying the following:

class ClockTest < ActiveSupport::TestCase
  test "it calls the job" do
    module ::Clockwork; module Methods; end; end
    Clockwork::Methods.any_instance.expects(:every).with(
      10.minutes,
      SomeBackgroundJob
    )
    require './clock.rb'
  end
end

With this code I am getting undefined method any_instance. I think it is because Clockword::Methods is a module and any_instance is only defined on classes. What is the proper Minitest/mocha approach to this? I just want to check that the method is called, I don't want it to actually run though.

Upvotes: 3

Views: 960

Answers (0)

Related Questions