3

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.

Vega
  • 27,856
  • 27
  • 95
  • 103
max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • Have you tried just setting the expectation on the module with something like ´Clockwork::Methods.expects(:every).with(10.minutes, SomeBackgroundJob)´? – max Nov 06 '17 at 22:51
  • 1
    @max in that case it shows that the method was never invoked. – max pleaner Nov 06 '17 at 23:26

0 Answers0