Search code examples
rubyminitest

How to test a method is called with Minitest?


class Post
  def save
    Mailer.notify!('bla')
    true
  end
end

How to test that when post.save is called, the Mailer.notify method is fired? and with the right arguments.

With RSpec I usually do:

expect(Mailer).to receive(:notify!).with('bla')
post.save

Thank you in advance!


Solution

  • You can do something like this:

    describe 'Post#save' do
      it "calls Mailer::notify!" do
        mock = MiniTest::Mock.new
        mock.expect(:call, nil, ['bla'])
    
        Mailer.stub(:notify!, mock) do
          post.save
        end
    
        mock.verify
      end
    end
    

    And yes, that is easier and more intuitive in RSpec...