Ruby: Mocking a class method with MiniTest?

This might not be helpful to you if you're stuck using 2.12.1, but looks like they added method stubbing to minitest/mock in HEAD here.

So, were you to update to minitest HEAD, I think you could do this:

product = Product.new
Product.stub(:find, product) do
  assert_equal product, Product.find(1)
end

What I do is that I simply stub the class method and replace it with my own lambda function which proves that original function was called. You can also test what arguments were used.

Example:

  test "unsubscribe user" do
    user = create(:user, password: "Secret1", email: "[email protected]", confirmation_token: "token", newsletter_check: false)

    newsletter = create(:newsletter, name: "Learnlife News")
    unsubscribe_function = -> (email:) { @unsubscribed_email = email }

    Hubspot::Subscription.stub :unsubscribe_all, unsubscribe_function do
      get user_confirmation_en_path(confirmation_token: "token")
    end

    assert_equal @unsubscribed_email, "[email protected]"
  end