Kleber S.
Kleber S.

Reputation: 8260

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!

Upvotes: 5

Views: 5027

Answers (2)

Cristian Rennella
Cristian Rennella

Reputation: 727

For mocking with minitest I like using Mocha gem https://github.com/freerange/mocha

I will be:

Mailer.expects(:notify!).with('bla').at_least_once

Upvotes: 2

spickermann
spickermann

Reputation: 107142

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...

Upvotes: 9

Related Questions